summaryrefslogtreecommitdiff
path: root/scripts/email_bookmarks.php
blob: 4b616e745e11a2e6d53cfbe8e10c36f57432bca6 (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
<?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_slice($urls, 0, $config['num_urls_to_read']);

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

$email = $config['email'];

mail(
    $email['to'], 
    $email['subject'],
    buildMessage($urls, $config['site']),
    [
        'From'         => $email['from'],
        'MIME-Version' => '1.0',
        'Content-Type' => 'text/html; charset=UTF-8',
    ]
);

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;
}