Skip to content

Commit

Permalink
MDL-69265 email: Add admin setting for email headers
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Aug 12, 2020
1 parent a7d9b53 commit 0f89884
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion admin/settings/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,11 @@
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);

Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
$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.';
Expand Down Expand Up @@ -516,6 +517,7 @@
$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';
Expand Down
9 changes: 9 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions lib/tests/moodlelib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3220,6 +3220,32 @@ public function test_generate_email_messageid($wwwroot, $msgids) {
}
}

/**
* 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
*/
Expand Down

0 comments on commit 0f89884

Please sign in to comment.