diff --git a/admin/settings/server.php b/admin/settings/server.php index c1b20560e0f..e86bbc95101 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -363,8 +363,11 @@ $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'), + +$temp->add(new admin_setting_configtext('emailsubjectprefix', new lang_string('emailsubjectprefix', 'admin'), new lang_string('configemailsubjectprefix', 'admin'), '', PARAM_RAW)); +$temp->add(new admin_setting_configtextarea('emailheaders', new lang_string('emailheaders', 'admin'), + new lang_string('configemailheaders', 'admin'), '', PARAM_RAW, '50', '3')); $ADMIN->add('email', $temp); diff --git a/lang/en/admin.php b/lang/en/admin.php index 7fff4c0fbb3..5cb42af68cc 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -228,6 +228,7 @@ $string['configeditorfontlist'] = 'Select the fonts that should appear in the ed $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['configemailheaders'] = 'Raw email headers to be added verbatum to all outgoing email.'; $string['configenablecalendarexport'] = 'Enable exporting or subscribing to calendars.'; $string['configenablecomments'] = 'Enable comments'; $string['configenablecourserequests'] = 'If enabled, users with the capability to request new courses (moodle/course:request) will have the option to request a course. This capability is not allowed for any of the default roles. It may be applied in the system or category context.'; @@ -516,6 +517,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['emailheaders'] = 'Email headers'; $string['emailsubjectprefix'] = 'Email subject prefix text'; $string['emoticontext'] = 'Text'; $string['emoticonimagename'] = 'Image name'; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 0418097c993..c4cab7d5dbf 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6221,6 +6221,15 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $mail->addCustomHeader('X-Moodle-Originating-Script: ' . $originheader); } + if (!empty($CFG->emailheaders)) { + $headers = array_map('trim', explode("\n", $CFG->emailheaders)); + foreach ($headers as $header) { + if (!empty($header)) { + $mail->addCustomHeader($header); + } + } + } + if (!empty($from->priority)) { $mail->Priority = $from->priority; } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 1648a8fb9fb..3f40d078d4d 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -3220,6 +3220,32 @@ class core_moodlelib_testcase extends advanced_testcase { } } + /** + * Test email with custom headers + */ + public function test_send_email_with_custom_header() { + global $DB, $CFG; + $this->preventResetByRollback(); + $this->resetAfterTest(); + + $touser = $this->getDataGenerator()->create_user(); + $fromuser = $this->getDataGenerator()->create_user(); + $fromuser->customheaders = 'X-Custom-Header: foo'; + + set_config('allowedemaildomains', 'example.com'); + set_config('emailheaders', 'X-Fixed-Header: bar'); + + $sink = $this->redirectEmails(); + email_to_user($touser, $fromuser, 'subject', 'message'); + + $emails = $sink->get_messages(); + $this->assertCount(1, $emails); + $email = reset($emails); + $this->assertContains('X-Custom-Header: foo', $email->header); + $this->assertContains("X-Fixed-Header: bar", $email->header); + $sink->clear(); + } + /** * A data provider for testing email diversion */