From 9ffbb786686a046730a8c7047f14ef63f5e450bf Mon Sep 17 00:00:00 2001 From: Paul Damiani Date: Thu, 10 Jan 2019 17:18:00 +1100 Subject: [PATCH] MDL-63569 email: New setting to prefix the subject of outgoing emails --- admin/settings/server.php | 2 ++ lang/en/admin.php | 2 ++ lib/moodlelib.php | 1 + lib/templates/email_html.mustache | 2 ++ lib/templates/email_subject.mustache | 4 ++- lib/templates/email_text.mustache | 2 ++ lib/tests/message_test.php | 41 ++++++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 1 deletion(-) diff --git a/admin/settings/server.php b/admin/settings/server.php index 8c47a56b77c..a869ce6294d 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -354,6 +354,8 @@ $choices = array(new lang_string('never', 'admin'), new lang_string('onlynoreply', 'admin')); $temp->add(new admin_setting_configselect('emailfromvia', new lang_string('emailfromvia', 'admin'), new lang_string('configemailfromvia', 'admin'), 1, $choices)); + $temp->add(new admin_setting_configtext('emailsubjectprefix', new lang_string('emailsubjectprefix', 'admin'), + new lang_string('configemailsubjectprefix', 'admin'), '', PARAM_RAW)); $ADMIN->add('email', $temp); diff --git a/lang/en/admin.php b/lang/en/admin.php index 34ed615a5d3..9e85af86834 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -223,6 +223,7 @@ $string['configeditordictionary'] = 'This value will be used if aspell doesn\'t $string['configeditorfontlist'] = 'Select the fonts that should appear in the editor\'s drop-down list.'; $string['configemailchangeconfirmation'] = 'Require an email confirmation step when users change their email address in their profile.'; $string['configemailfromvia'] = 'Add via information in the "From" section of outgoing email. This informs the recipient from where this email came from and also helps combat recipients accidentally replying to no-reply email addresses.'; +$string['configemailsubjectprefix'] = 'Text to be prefixed to the subject line of all outgoing mail.'; $string['configenablecalendarexport'] = 'Enable exporting or subscribing to calendars.'; $string['configenablecomments'] = 'Enable comments'; $string['configenablecourserequests'] = 'This will allow any user to request a course be created.'; @@ -499,6 +500,7 @@ $string['editorspellinghelp'] = 'Enable or disable spell-checking. When enabled, $string['editstrings'] = 'Edit words or phrases'; $string['emailchangeconfirmation'] = 'Email change confirmation'; $string['emailfromvia'] = 'Email via information'; +$string['emailsubjectprefix'] = 'Email subject prefix text'; $string['emoticontext'] = 'Text'; $string['emoticonimagename'] = 'Image name'; $string['emoticonalt'] = 'Alternative text'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index fc88a137b89..b383cda03d3 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6047,6 +6047,7 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', 'siteshortname' => $SITE->shortname, 'sitewwwroot' => $CFG->wwwroot, 'subject' => $subject, + 'prefix' => $CFG->emailsubjectprefix, 'to' => $user->email, 'toname' => fullname($user), 'from' => $mail->From, diff --git a/lib/templates/email_html.mustache b/lib/templates/email_html.mustache index a8d93d98f7f..562e22bc397 100644 --- a/lib/templates/email_html.mustache +++ b/lib/templates/email_html.mustache @@ -34,9 +34,11 @@ * replyto * replytoname * body + * prefix Example context (json): { + "prefix": "[Prefix Text]", "body": "Email body" } }} diff --git a/lib/templates/email_subject.mustache b/lib/templates/email_subject.mustache index 8454983e23d..88deb2e3883 100644 --- a/lib/templates/email_subject.mustache +++ b/lib/templates/email_subject.mustache @@ -31,10 +31,12 @@ * fromname * replyto * replytoname + * prefix Example context (json): { + "prefix": "[Prefix Text]", "subject": "Email subject" } }} -{{{subject}}} +{{#prefix}}{{{prefix}}} {{/prefix}}{{{subject}}} diff --git a/lib/templates/email_text.mustache b/lib/templates/email_text.mustache index 7e537c11f32..6c55c305b93 100644 --- a/lib/templates/email_text.mustache +++ b/lib/templates/email_text.mustache @@ -33,9 +33,11 @@ * replyto * replytoname * body + * prefix Example context (json): { + "prefix": "[Prefix Text]", "body": "Email body" } }} diff --git a/lib/tests/message_test.php b/lib/tests/message_test.php index d2e02658efd..fd3023aa33b 100644 --- a/lib/tests/message_test.php +++ b/lib/tests/message_test.php @@ -225,4 +225,45 @@ class core_message_testcase extends advanced_testcase { $eventsink->close(); $sink->close(); } + + public function test_send_message_with_prefix() { + global $DB, $CFG; + $this->preventResetByRollback(); + $this->resetAfterTest(); + + $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1)); + $user2 = $this->getDataGenerator()->create_user(); + set_config('allowedemaildomains', 'example.com'); + set_config('emailsubjectprefix', '[Prefix Text]'); + + // Test basic email processor. + $this->assertFileExists("$CFG->dirroot/message/output/email/version.php"); + $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php"); + + $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'"); + set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2); + + // Check that prefix is ammended to the subject of the email. + $message = new \core\message\message(); + $message->courseid = 1; + $message->component = 'moodle'; + $message->name = 'instantmessage'; + $message->userfrom = $user1; + $message->userto = $user2; + $message->subject = get_string('unreadnewmessage', 'message', fullname($user1)); + $message->fullmessage = 'message body'; + $message->fullmessageformat = FORMAT_MARKDOWN; + $message->fullmessagehtml = '

message body

'; + $message->smallmessage = 'small message'; + $message->notification = '0'; + $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); + $message->set_additional_content('email', $content); + $sink = $this->redirectEmails(); + $messageid = message_send($message); + $emails = $sink->get_messages(); + $this->assertCount(1, $emails); + $email = reset($emails); + $this->assertSame('[Prefix Text] '. get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject); + $sink->clear(); + } }