Skip to content

Commit

Permalink
Add HTML email support to alert emails.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Oct 19, 2021
1 parent d1c0cce commit 38ca3dc
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 35 deletions.
55 changes: 36 additions & 19 deletions scripts/alertmailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function mlog($message) {

$current = array('email' => '', 'token' => '');
$email_text = '';
$html_text = '';
$globalsuccess = 1;

# Fetch all confirmed, non-deleted alerts
Expand Down Expand Up @@ -178,11 +179,12 @@ function mlog($message) {

if ($email != $current['email']) {
if ($email_text) {
write_and_send_email($current, $email_text, $template);
write_and_send_email($current, $email_text, $html_text, $template);
}
$current['email'] = $email;
$current['token'] = $alertitem['alert_id'] . '-' . $alertitem['registrationtoken'];
$email_text = '';
$html_text = '';
$q = $db->query('SELECT user_id FROM users WHERE email = :email', array(
':email' => $email
))->first();
Expand Down Expand Up @@ -253,7 +255,7 @@ function mlog($message) {
} else {
$text .= "$vote$teller";
}
$text .= " (division #$num; result was " . $row['yes_total'] . ' aye, ' . $row['no_total'] . ' no)';
$text .= " (division #$num; result was <b>" . $row['yes_total'] . '</b> aye, <b>' . $row['no_total'] . ' no</b>)';
$data['rows'][] = [
'parent' => [
'body' => $row['division_title'],
Expand All @@ -275,7 +277,7 @@ function mlog($message) {
if ($major !== $row['major']) {
$count[$major] = $total; $total = 0;
$major = $row['major'];
$o[$major] = '';
$o[$major] = ['text' => '', 'html' => ''];
$k = 3;
}
#mlog($row['major'] . " " . $row['gid'] ."\n");
Expand All @@ -302,38 +304,52 @@ function mlog($message) {
if ($major == 'V' || $k >= 0) {
$any_content = true;
$parentbody = text_html_to_email($row['parent']['body']);
$body = text_html_to_email($row['extract']);
$body_text = text_html_to_email($row['extract']);
$body_html = $row['extract'];
if (isset($row['speaker']) && count($row['speaker'])) {
$body = $row['speaker']['name'] . ': ' . $body;
$body_text = $row['speaker']['name'] . ': ' . $body_text;
$body_html = '<strong style="font-weight: 900;">' . $row['speaker']['name'] . '</strong>: ' . $body_html;
}
$body_html = '<p style="font-size: 16px;">' . $body_html . '</p>';

$body = wordwrap($body, 72);
$o[$major] .= $parentbody . ' (' . format_date($row['hdate'], SHORTDATEFORMAT) . ")\nhttps://www.theyworkforyou.com" . $row['listurl'] . "\n";
$o[$major] .= $body . "\n\n";
$body_text = wordwrap($body_text, 72);
$o[$major]['text'] .= $parentbody . ' (' . format_date($row['hdate'], SHORTDATEFORMAT) . ")\nhttps://www.theyworkforyou.com" . $row['listurl'] . "\n";
$o[$major]['text'] .= $body_text . "\n\n";
$o[$major]['html'] .= '<a href="https://www.theyworkforyou.com' . $row['listurl'] . '"><h2 style="line-height: 1.2; font-size: 17px; font-weight: 900;">' . $parentbody . '</h2></a> <span style="margin: 16px 0 0 0; display: block; font-size: 16px;">' . format_date($row['hdate'], SHORTDATEFORMAT) . '</span>';
$o[$major]['html'] .= $body_html . "\n\n";
}
$total++;
}
$count[$major] = $total;

if ($any_content) {
# Add data to email_text
# Add data to email_text/html_text
$desc = trim(html_entity_decode($data['searchdescription']));
$desc = trim(preg_replace(['#\(B\d+( OR B\d+)*\)#', '#B\d+( OR B\d+)*#'], '', $desc));
foreach ($o as $major => $body) {
if ($body) {
$heading = $desc . ' : ' . $count[$major] . ' ' . $sects[$major] . ($count[$major] != 1 ? 's' : '');
$email_text .= "$heading\n" . str_repeat('=', strlen($heading)) . "\n\n";
if ($body['text']) {
$heading_text = $desc . ' : ' . $count[$major] . ' ' . $sects[$major] . ($count[$major] != 1 ? 's' : '');
$heading_html = $desc . ' : <strong>' . $count[$major] . '</strong> ' . $sects[$major] . ($count[$major] != 1 ? 's' : '');

$email_text .= "$heading_text\n" . str_repeat('=', strlen($heading_text)) . "\n\n";
if ($count[$major] > 3 && $major != 'V') {
$email_text .= "There are more results than we have shown here. See more:\nhttps://www.theyworkforyou.com/search/?s=" . urlencode($criteria_raw) . "+section:" . $sects_search[$major] . "&o=d\n\n";
}
$email_text .= $body;
$email_text .= $body['text'];

$html_text .= '<hr style="height:2px;border-width:0; background-color: #f7f6f5; margin: 30px 0;">';
$html_text .= '<p style="font-size:16px;">' . $heading_html . '</p>';
if ($count[$major] > 3 && $major != 'V') {
$html_text .= '<p style="font-size:16px;">There are more results than we have shown here. <a href="https://www.theyworkforyou.com/search/?s=' . urlencode($criteria_raw) . "+section:" . $sects_search[$major] . '&o=d">See more</a></p>';
}
$html_text .= $body['html'];
}
}
}
}
}
if ($email_text) {
write_and_send_email($current, $email_text, $template);
write_and_send_email($current, $email_text, $html_text, $template);
}

mlog("\n");
Expand Down Expand Up @@ -402,15 +418,16 @@ function sort_by_stuff($a, $b) {
return ($a['hpos'] > $b['hpos']) ? 1 : -1;
}

function write_and_send_email($current, $data, $template) {
function write_and_send_email($current, $text, $html, $template) {
global $globalsuccess, $sentemails, $nomail, $start_time;

$data .= '====================';
$text .= '====================';
$sentemails++;
mlog("SEND $sentemails : Sending email to $current[email] ... ");
$d = array('to' => $current['email'], 'template' => $template);
$m = array(
'DATA' => $data,
'DATA' => $text,
'_HTML_' => $html,
'MANAGE' => 'https://www.theyworkforyou.com/D/' . $current['token'],
);
if (!$nomail) {
Expand All @@ -423,7 +440,7 @@ function write_and_send_email($current, $data, $template) {
sleep(1);
}
} else {
mlog($data);
mlog($text);
$success = 1;
}
mlog("done\n");
Expand All @@ -433,7 +450,7 @@ function write_and_send_email($current, $data, $template) {
}

function text_html_to_email($s) {
$s = preg_replace('#</?(i|small)>#', '', $s);
$s = preg_replace('#</?(i|b|small)>#', '', $s);
$s = preg_replace('#</?span[^>]*>#', '*', $s);
$s = str_replace(
array('&#163;', '&#8211;', '&#8212;', '&#8217;', '<br>'),
Expand Down
162 changes: 162 additions & 0 deletions www/includes/easyparliament/templates/emails/alert_mailout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Your TheyWorkForYou email alert</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<style type="text/css">
/* CLIENT-SPECIFIC STYLES */
body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
img { -ms-interpolation-mode: bicubic; }

/* RESET STYLES */
img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; }
table { border-collapse: collapse !important; }
body { height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }

/* iOS BLUE LINKS */
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
}

/* GMAIL BLUE LINKS */
u + #body a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}

/* SAMSUNG MAIL BLUE LINKS */
#MessageViewBody a {
color: inherit;
text-decoration: none;
font-size: inherit;
font-family: inherit;
font-weight: inherit;
line-height: inherit;
}

/* These rules set the link and hover states, making it clear that links are, in fact, links. */
/* Embrace established conventions like underlines on links to keep emails accessible. */
a { color: #4faded; font-weight: 900; text-decoration: underline; }
a:hover { color: #38a2eb !important; text-decoration: none !important; }
a.button:hover { color: #ffffff !important; background-color: #38a2eb !important; }

/* These rules adjust styles for desktop devices, keeping the email responsive for users. */
/* Some email clients don't properly apply media query-based styles, which is why we go mobile-first. */
@media screen and (min-width:600px) {
h1 { font-size: 36px !important; line-height: 1.2 !important; margin: 0 0 28px 0;}
.intro { font-size: 24px !important; line-height: 36px !important; }

h2 { font-size: 25px !important; line-height: 1.2 !important; margin: 0; }

p { font-size: 18px !important;}

}

@font-face {
font-family: 'Source Sans Pro';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xK3dSBYKcSV-LCoeQqfX1RYOo3qOK7lujVj9w.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

@font-face {
font-family: 'Source Sans Pro';
font-style: italic;
font-weight: 900;
font-display: swap;
src: url(https://fonts.gstatic.com/s/sourcesanspro/v14/6xKwdSBYKcSV-LCoeQqfX1RYOo3qPZZklyds18S0xR41.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

u + .body .glist { margin-left: 0 !important; }
ul li { margin-bottom: 20px; color: #ffffff; }

@media only screen and (max-width: 600px) {
u + .body .glist { margin-left: 25px !important; }
}
</style>

</head>
<body style="margin: 0 !important; padding: 0 !important; background-color: #f7f6f5;" class="body">

<!--[if (gte mso 9)|(IE)]>
<table cellspacing="0" cellpadding="0" border="0" width="600" align="center" role="presentation"><tr><td>
<![endif]-->

<div role="article" aria-label="An email from TheyWorkForYou" lang="en" style="background-color: white; color: #2b2b2b; font-family: 'Lato', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 18px; font-weight: 400; line-height: 28px; margin: 0 auto; max-width: 600px; padding: 40px 20px 40px 20px;">

<header>
<a href="https://www.theyworkforyou.com">
<img src="images/theyworkforyou-logo.png" alt="" width="170">
</a>
<h1 style="color: #333333; font-size: 21px; font-weight: 400; line-height: 21px; font-family: 'Source Sans Pro'; margin-top: 30px;">Email alerts</h1>
</header>

<main>

{_HTML_}

<hr style="height:2px;border-width:0; background-color: #f7f6f5; margin: 30px 0;">

<p style="font-size: 16px; margin-top: 30px;">Best wishes,<br>TheyWorkForYou</p>

<p style="font-size: 16px; margin-top: 30px;">
mySociety,<br>
483 Green Lanes,<br>
London,<br>
N13 4BS,<br>
United Kingdom
</p>

<div class="list-box" style="background-color: #f7f6f5; padding: 20px; border-radius: 5px; margin-top: 30px;">
<h3 style="line-height: 1.2; color: #333333;">Help keep TheyWorkForYou running</h3>
<p>Please consider donating to mySociety to help us continue and expand:</p>

<div style="margin: 0;"><!--[if mso]>
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="https://www.mysociety.org/twfy-donate/" style="height:60px;v-text-anchor:middle;width:300px;" arcsize="10%" stroke="f" fillcolor="#0BA7D1">
<w:anchorlock/>
<center>
<![endif]-->
<a class="button" href="https://www.mysociety.org/twfy-donate/"
style="background-color:#4faded;border-radius:4px;color:#ffffff;display:inline-block;font-size:18px;font-weight:900;line-height:60px;text-align:center;text-decoration:none;padding: 0 20px;-webkit-text-size-adjust:none;">Donate</a>
<!--[if mso]>
</center>
</v:roundrect>
<![endif]--></div>
</div>

<hr style="height:2px;border-width:0; background-color: #f7f6f5; margin: 30px 0;">

</main>

<footer style="margin-top: 30px;">
<a href="https://www.mysociety.org">
<img src="images/mySociety-logo.png" alt="" width="151" height="auto">
</a>

<hr style="height:2px;border-width:0; background-color: #f7f6f5; margin: 30px 0;">

<p><a href="{MANAGE}">Unsubscribe, manage or add to your alerts</a>.</p>

</footer>

</div>
<!--[if (gte mso 9)|(IE)]>
</td></tr></table>
<![endif]-->
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ continue and expand:
https://www.mysociety.org/twfy-donate/
-------------------------------------------------------------------

To manage, unsubscribe or add to your alerts, please visit the following link:
To unsubscribe, manage or add to your alerts, please visit the following link:
{MANAGE}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 38ca3dc

Please sign in to comment.