summaryrefslogtreecommitdiff
path: root/scripts/email_bookmarks.php
blob: 891ab4dd3538cfc36d235efaa4be4feb007c0bfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php declare(strict_types=1);

error_reporting(E_ALL);

$config = require_once(__DIR__.'/../config.php');

$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES);

$urls = array_reverse($urls);

$urls = array_slice($urls, 0, 5);

if (!count($urls)) {
    exit();
}

$email = $config['email'];

$message = buildMessage($urls, $config['site']);

$headers = implode("\r\n", [
    "From: {$email['from']}",
    'MIME-Version: 1.0',
    'Content-Type: text/html; charset=UTF-8',
]);

mail($email['to'], $email['subject'], $message, $headers);

function buildMessage(array $urls, string $site): string
{
    $urls = array_map(function ($url) use ($site) {
        return sprintf(
            '<li><a href="%s/bookmarks/read?url=%s">%s</a></li>',
            $site,
            urlencode($url),
            htmlentities($url)
        );
    }, $urls);

    $urls = sprintf('<ol>%s</ol>', implode('', $urls));

    return <<< EOF_HTML
<html>
    <body>
        <p>Below are today's bookmarks for reading.</p>
        $urls
    </body>
</html>
EOF_HTML;
}