diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/email_bookmarks.php | 50 | 
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/email_bookmarks.php b/scripts/email_bookmarks.php new file mode 100644 index 0000000..891ab4d --- /dev/null +++ b/scripts/email_bookmarks.php @@ -0,0 +1,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; +}  | 
