Merge branch 'MDL-69265-email-headers' of https://github.com/brendanheywood/moodle

This commit is contained in:
Andrew Nicols 2020-08-13 07:28:27 +08:00
commit 78193d34b4
4 changed files with 41 additions and 1 deletions

View File

@ -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);

View File

@ -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';

View File

@ -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;
}

View File

@ -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
*/