MDL-71515 admin: Improve Test outgoing mail config
Improve the Test outgoing mail configuration admin page adding: - Timestamp to the subject and message text. - Additional subject text to the current text subject. - From field (username or email) to send the test email from the that Moodle user.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
+6
-2
@@ -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.<br />{$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.<br />From: {$a->fromemail}<br />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.';
|
||||
|
||||
Reference in New Issue
Block a user