From efd420608eff5d9443703d3867b4cc6256e436c5 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Tue, 16 Feb 2016 15:21:52 +1100 Subject: [PATCH] MDL-52990 email: Added site wide email mustache templates --- lib/moodlelib.php | 49 +++++++++++++++++-- lib/templates/email_fromname.mustache | 35 +++++++++++++ lib/templates/email_html.mustache | 38 ++++++++++++++ lib/templates/email_subject.mustache | 35 +++++++++++++ lib/templates/email_text.mustache | 37 ++++++++++++++ mod/forum/lib.php | 10 +--- .../forum_post_email_htmlemail.mustache | 4 -- 7 files changed, 193 insertions(+), 15 deletions(-) create mode 100644 lib/templates/email_fromname.mustache create mode 100644 lib/templates/email_html.mustache create mode 100644 lib/templates/email_subject.mustache create mode 100644 lib/templates/email_text.mustache diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 9233dbb8cc9..3ab1da3e07d 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5480,7 +5480,7 @@ function email_should_be_diverted($email) { function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $attachment = '', $attachname = '', $usetrueaddress = true, $replyto = '', $replytoname = '', $wordwrapwidth = 79) { - global $CFG; + global $CFG, $PAGE, $SITE; if (empty($user) or empty($user->id)) { debugging('Can not send email to null user', DEBUG_DEVELOPER); @@ -5598,8 +5598,6 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $tempreplyto[] = array($replyto, $replytoname); } - $mail->Subject = substr($subject, 0, 900); - $temprecipients[] = array($user->email, fullname($user)); // Set word wrap. @@ -5639,6 +5637,51 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $mail->Priority = $from->priority; } + $renderer = $PAGE->get_renderer('core'); + $context = array( + 'sitefullname' => $SITE->fullname, + 'siteshortname' => $SITE->shortname, + 'sitewwwroot' => $CFG->wwwroot, + 'subject' => $subject, + 'to' => $user->email, + 'toname' => fullname($user), + 'from' => $mail->From, + 'fromname' => $mail->FromName, + ); + if (!empty($tempreplyto[0])) { + $context['replyto'] = $tempreplyto[0][0]; + $context['replytoname'] = $tempreplyto[0][1]; + } + if ($user->id > 0) { + $context['touserid'] = $user->id; + $context['tousername'] = $user->username; + } + + if (!empty($user->mailformat) && $user->mailformat == 1) { + // Only process html templates if the user preferences allow html email. + + if ($messagehtml) { + // If html has been given then pass it through the template. + $context['body'] = $messagehtml; + $messagehtml = $renderer->render_from_template('core/email_html', $context); + + } else { + // If no html has been given, BUT there is an html wrapping template then + // auto convert the text to html and then wrap it. + $autohtml = trim(text_to_html($messagetext)); + $context['body'] = $autohtml; + $temphtml = $renderer->render_from_template('core/email_html', $context); + if ($autohtml != $temphtml) { + $messagehtml = $temphtml; + } + } + } + + $context['body'] = $messagetext; + $mail->Subject = $renderer->render_from_template('core/email_subject', $context); + $mail->FromName = $renderer->render_from_template('core/email_fromname', $context); + $messagetext = $renderer->render_from_template('core/email_text', $context); + if ($messagehtml && !empty($user->mailformat) && $user->mailformat == 1) { // Don't ever send HTML to users who don't want it. $mail->isHTML(true); diff --git a/lib/templates/email_fromname.mustache b/lib/templates/email_fromname.mustache new file mode 100644 index 00000000000..ec43023a985 --- /dev/null +++ b/lib/templates/email_fromname.mustache @@ -0,0 +1,35 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core/email_html + + Template for all email subjects. + + Context variables required for this template: + * sitefullname + * siteshortname + * sitewwwroot + * subject + * to + * toname + * touserid + * from + * fromname + * replyto + * replytoname +}} +{{{fromname}}} diff --git a/lib/templates/email_html.mustache b/lib/templates/email_html.mustache new file mode 100644 index 00000000000..e8c1aaaf593 --- /dev/null +++ b/lib/templates/email_html.mustache @@ -0,0 +1,38 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core/email_html + + Template for all html emails. Note that it may wrap content formatted + elsewhere in another a module template. + + Context variables required for this template: + * sitefullname + * siteshortname + * sitewwwroot + * subject + * to + * toname + * touserid + * tousername + * from + * fromname + * replyto + * replytoname + * body +}} +{{{body}}} diff --git a/lib/templates/email_subject.mustache b/lib/templates/email_subject.mustache new file mode 100644 index 00000000000..c67afdd263e --- /dev/null +++ b/lib/templates/email_subject.mustache @@ -0,0 +1,35 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core/email_html + + Template for all email subjects. + + Context variables required for this template: + * sitefullname + * siteshortname + * sitewwwroot + * subject + * to + * toname + * touserid + * from + * fromname + * replyto + * replytoname +}} +{{{subject}}} diff --git a/lib/templates/email_text.mustache b/lib/templates/email_text.mustache new file mode 100644 index 00000000000..8ffc85a853d --- /dev/null +++ b/lib/templates/email_text.mustache @@ -0,0 +1,37 @@ +{{! + This file is part of Moodle - http://moodle.org/ + + Moodle is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Moodle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Moodle. If not, see . +}} +{{! + @template core/email_text + + Template for all html emails. Note that it may wrap content formatted + elsewhere in another a module template. + + Context variables required for this template: + * sitefullname + * siteshortname + * sitewwwroot + * subject + * to + * toname + * touserid + * from + * fromname + * replyto + * replytoname + * body +}} +{{{body}}} diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 4212b75835a..b8a426cb0f7 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1004,13 +1004,8 @@ function forum_cron() { $posttext = get_string('digestmailheader', 'forum', $headerdata)."\n\n"; $headerdata->userprefs = ''.get_string('digestmailprefs', 'forum').''; - $posthtml = ""; -/* foreach ($CFG->stylesheets as $stylesheet) { - //TODO: MDL-21120 - $posthtml .= ''."\n"; - }*/ - $posthtml .= "\n\n"; - $posthtml .= '

'.get_string('digestmailheader', 'forum', $headerdata).'



'; + $posthtml = '

'.get_string('digestmailheader', 'forum', $headerdata).'

' + . '

'; foreach ($thesediscussions as $discussionid) { @@ -1157,7 +1152,6 @@ function forum_cron() { $posthtml .= "\n
" . implode(' ', $footerlinks) . '
'; $posthtml .= '

'; } - $posthtml .= ''; if (empty($userto->mailformat) || $userto->mailformat != 1) { // This user DOESN'T want to receive HTML diff --git a/mod/forum/templates/forum_post_email_htmlemail.mustache b/mod/forum/templates/forum_post_email_htmlemail.mustache index a30885df905..146e171f32b 100644 --- a/mod/forum/templates/forum_post_email_htmlemail.mustache +++ b/mod/forum/templates/forum_post_email_htmlemail.mustache @@ -77,9 +77,6 @@ "unsubscribediscussionlink": "https://example.com/mod/discussion/subscribe.php?id=2&d=2" } }} - - - -