summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-07-07 15:46:57 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-07-07 15:46:57 +0100
commit7656c1f90cd8ef2bc6f5bd3d26a583128dd749c7 (patch)
tree9ce390fd220bcbcc6301cc15cea5b66e6b962b73
parent0a3a8ca0ba903aa12face3c4b7273d4da288172a (diff)
Add email_bookmarks script
-rw-r--r--includes/functions.php2
-rw-r--r--scripts/email_bookmarks.php50
2 files changed, 51 insertions, 1 deletions
diff --git a/includes/functions.php b/includes/functions.php
index b5208e6..0118807 100644
--- a/includes/functions.php
+++ b/includes/functions.php
@@ -8,7 +8,7 @@ function respondAndExit(int $responseCode, string $header, array $headers = []):
header($header);
}
- header('Content-type: text/plain; charset=UTF-8');
+ header('Content-Type: text/plain; charset=UTF-8');
exit();
}
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;
+}