diff --git a/admin/classes/form/testoutgoingmailconf_form.php b/admin/classes/form/testoutgoingmailconf_form.php
index f344cfea587..251cdf04335 100644
--- a/admin/classes/form/testoutgoingmailconf_form.php
+++ b/admin/classes/form/testoutgoingmailconf_form.php
@@ -48,6 +48,17 @@ class testoutgoingmailconf_form extends \moodleform {
$mform->setType('recipient', PARAM_EMAIL);
$mform->addRule('recipient', get_string('required'), 'required');
+ // From user.
+ $options = ['maxlength' => '100', 'size' => '25'];
+ $mform->addElement('text', 'from', get_string('testoutgoingmailconf_fromemail', 'admin'), $options);
+ $mform->setType('from', PARAM_TEXT);
+ $mform->addHelpButton('from', 'testoutgoingmailconf_fromemail', 'admin');
+
+ // Additional subject text.
+ $options = ['size' => '25'];
+ $mform->addElement('text', 'additionalsubject', get_string('testoutgoingmailconf_subjectadditional', 'admin'), $options);
+ $mform->setType('additionalsubject', PARAM_TEXT);
+
$buttonarray = array();
$buttonarray[] = $mform->createElement('submit', 'send', get_string('testoutgoingmailconf_sendtest', 'admin'));
$buttonarray[] = $mform->createElement('cancel');
@@ -56,4 +67,26 @@ class testoutgoingmailconf_form extends \moodleform {
$mform->closeHeaderBefore('buttonar');
}
+
+ /**
+ * Validate Form field, should be a valid email format or a username that matches with a Moodle user.
+ *
+ * @param array $data
+ * @param array $files
+ * @return array
+ * @throws \dml_exception|\coding_exception
+ */
+ public function validation($data, $files): array {
+ $errors = parent::validation($data, $files);
+
+ if (isset($data['from']) && $data['from']) {
+ $userfrom = \core_user::get_user_by_username($data['from']);
+
+ if (!$userfrom && !validate_email($data['from'])) {
+ $errors['from'] = get_string('testoutgoingmailconf_fromemail_invalid', 'admin');
+ }
+ }
+
+ return $errors;
+ }
}
diff --git a/admin/testoutgoingmailconf.php b/admin/testoutgoingmailconf.php
index b1f9c58963a..8c2ea92721e 100644
--- a/admin/testoutgoingmailconf.php
+++ b/admin/testoutgoingmailconf.php
@@ -46,9 +46,36 @@ if ($data) {
$emailuser->email = $data->recipient;
$emailuser->id = -99;
- $subject = get_string('testoutgoingmailconf_subject', 'admin',
- format_string($SITE->fullname, true, ['context' => context_system::instance()]));
- $messagetext = get_string('testoutgoingmailconf_message', 'admin');
+ // Get the user who will send this email (From:).
+ $emailuserfrom = $USER;
+ if ($data->from) {
+ if (!$userfrom = \core_user::get_user_by_email($data->from)) {
+ $userfrom = \core_user::get_user_by_username($data->from);
+ }
+ if (!$userfrom && validate_email($data->from)) {
+ $dummyuser = \core_user::get_user(\core_user::NOREPLY_USER);
+ $dummyuser->id = -1;
+ $dummyuser->email = $data->from;
+ $dummyuser->firstname = $data->from;
+ $emailuserfrom = $dummyuser;
+ } else if ($userfrom) {
+ $emailuserfrom = $userfrom;
+ }
+ }
+
+ // Get the date the email will be sent.
+ $timestamp = userdate(time(), get_string('strftimedatetimeaccurate', 'core_langconfig'));
+
+ // Build the email subject.
+ $subjectparams = new stdClass();
+ $subjectparams->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
+ if (isset($data->additionalsubject)) {
+ $subjectparams->additional = format_string($data->additionalsubject);
+ }
+ $subjectparams->time = $timestamp;
+
+ $subject = get_string('testoutgoingmailconf_subject', 'admin', $subjectparams);
+ $messagetext = get_string('testoutgoingmailconf_message', 'admin', $timestamp);
// Manage Moodle debugging options.
$debuglevel = $CFG->debug;
@@ -60,7 +87,7 @@ if ($data) {
// Send test email.
ob_start();
- $success = email_to_user($emailuser, $USER, $subject, $messagetext);
+ $success = email_to_user($emailuser, $emailuserfrom, $subject, $messagetext);
$smtplog = ob_get_contents();
ob_end_clean();
@@ -76,7 +103,7 @@ if ($data) {
if ($success) {
$msgparams = new stdClass();
- $msgparams->fromemail = $USER->email;
+ $msgparams->fromemail = $emailuserfrom->email;
$msgparams->toemail = $emailuser->email;
$msg = get_string('testoutgoingmailconf_sentmail', 'admin', $msgparams);
$notificationtype = 'notifysuccess';
diff --git a/lang/en/admin.php b/lang/en/admin.php
index 7a7f667e7a2..d887a41854d 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -1354,11 +1354,15 @@ $string['tempdatafoldercleanup'] = 'Clean up temporary data files older than';
$string['templates'] = 'Templates';
$string['testoutgoingmailconf'] = 'Test outgoing mail configuration';
$string['testoutgoingmaildetail'] = 'Note: Before testing, please save your configuration.
{$a}';
-$string['testoutgoingmailconf_message'] = 'This is a test message to confirm that you have successfully configured your site\'s outgoing mail.';
$string['testoutgoingmailconf_errorcommunications'] = 'Your site couldn\'t communicate with your mail server. Please check your outgoing mail configuration.';
+$string['testoutgoingmailconf_message'] = "This is a test message to confirm that you have successfully configured your site's outgoing mail.\n\n Sent:" . '{$a}';
+$string['testoutgoingmailconf_fromemail'] = 'From username or email address';
+$string['testoutgoingmailconf_fromemail_help'] = 'This field emulates sending the message from that user, but the From header used in the real email sent will depend on other settings such as allowedemaildomains';
+$string['testoutgoingmailconf_fromemail_invalid'] = 'Invalid From username or email. Must be a valid email format or an existing username in Moodle.';
$string['testoutgoingmailconf_sendtest'] = 'Send a test message';
$string['testoutgoingmailconf_sentmail'] = 'This site has successfully sent a test message to the mail server.
From: {$a->fromemail}
To: {$a->toemail}';
-$string['testoutgoingmailconf_subject'] = '{$a}: test message';
+$string['testoutgoingmailconf_subject'] = '{$a->site}: test message. {$a->additional} Sent: {$a->time}';
+$string['testoutgoingmailconf_subjectadditional'] = 'Additional subject';
$string['testoutgoingmailconf_toemail'] = 'To email address';
$string['themedesignermode'] = 'Theme designer mode';
$string['themedesignermodewarning'] = 'Theme designer mode is enabled. This should not be enabled on production sites as it can significantly reduce performance.';