diff --git a/admin/classes/form/testoutgoingmailconf_form.php b/admin/classes/form/testoutgoingmailconf_form.php new file mode 100644 index 00000000000..fbf8ac4fb1c --- /dev/null +++ b/admin/classes/form/testoutgoingmailconf_form.php @@ -0,0 +1,59 @@ +. + +/** + * Testing outgoing mail configuration form + * + * @package core + * @copyright 2019 Victor Deniz + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_admin\form; + +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/formslib.php'); + +/** + * Test mail form + * + * @package core + * @copyright 2019 Victor Deniz + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class testoutgoingmailconf_form extends \moodleform { + /** + * Add elements to form + */ + public function definition() { + $mform = $this->_form; + + // Recipient. + $options = ['maxlength' => '100', 'size' => '25']; + $mform->addElement('text', 'recipient', get_string('testoutgoingmailconf_toemail', 'admin'), $options); + $mform->setType('recipient', PARAM_EMAIL); + $mform->addRule('recipient', get_string('required'), 'required'); + + $buttonarray = array(); + $buttonarray[] = $mform->createElement('submit', 'send', get_string('testoutgoingmailconf_sendtest', 'admin')); + $buttonarray[] = $mform->createElement('cancel'); + + $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false); + $mform->closeHeaderBefore('buttonar'); + + } +} diff --git a/admin/settings/server.php b/admin/settings/server.php index 96c7871dd6e..8c47a56b77c 100644 --- a/admin/settings/server.php +++ b/admin/settings/server.php @@ -176,6 +176,8 @@ $ADMIN->add('server', $temp); $ADMIN->add('server', new admin_externalpage('environment', new lang_string('environment','admin'), "$CFG->wwwroot/$CFG->admin/environment.php")); $ADMIN->add('server', new admin_externalpage('phpinfo', new lang_string('phpinfo'), "$CFG->wwwroot/$CFG->admin/phpinfo.php")); +$ADMIN->add('server', new admin_externalpage('testoutgoingmailconf', new lang_string('testoutgoingmailconf', 'admin'), + new moodle_url("$CFG->wwwroot/$CFG->admin/testoutgoingmailconf.php"), 'moodle/site:config', true)); // "performance" settingpage @@ -326,6 +328,10 @@ $temp->add(new admin_setting_configtextarea('allowedemaildomains', new lang_string('allowedemaildomains', 'admin'), new lang_string('configallowedemaildomains', 'admin'), '')); +$url = new moodle_url('/admin/testoutgoingmailconf.php'); +$link = html_writer::link($url, get_string('testoutgoingmailconf', 'admin')); +$temp->add(new admin_setting_heading('testoutgoinmailc', new lang_string('testoutgoingmailconf', 'admin'), + new lang_string('testoutgoingmaildetail', 'admin', $link))); $temp->add(new admin_setting_heading('emaildoesnotfit', new lang_string('doesnotfit', 'admin'), new lang_string('doesnotfitdetail', 'admin'))); $charsets = get_list_of_charsets(); diff --git a/admin/testoutgoingmailconf.php b/admin/testoutgoingmailconf.php new file mode 100644 index 00000000000..ce5857f4ee4 --- /dev/null +++ b/admin/testoutgoingmailconf.php @@ -0,0 +1,92 @@ +. + +/** + * Test output mail configuration page + * + * @copyright 2019 Victor Deniz , based on Michael Milette code + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(__DIR__ . '/../config.php'); +require_once($CFG->libdir.'/adminlib.php'); + +// This is an admin page. +admin_externalpage_setup('testoutgoingmailconf'); + +$headingtitle = get_string('testoutgoingmailconf', 'admin'); +$homeurl = new moodle_url('/admin/category.php', array('category' => 'email')); +$returnurl = new moodle_url('/admin/testoutgoingconf.php'); + +$form = new core_admin\form\testoutgoingmailconf_form(null, ['returnurl' => $returnurl]); +if ($form->is_cancelled()) { + redirect($homeurl); +} + +// Display the page. +echo $OUTPUT->header(); +echo $OUTPUT->heading($headingtitle); + +$data = $form->get_data(); +if ($data) { + $emailuser = new stdClass(); + $emailuser->email = $data->recipient; + $emailuser->id = -99; + + $subject = get_string('testoutgoingmailconf_subject', 'admin', $SITE->fullname); + $messagetext = get_string('testoutgoingmailconf_message', 'admin'); + + // Manage Moodle debugging options. + $debuglevel = $CFG->debug; + $debugdisplay = $CFG->debugdisplay; + $debugsmtp = $CFG->debugsmtp; + $CFG->debugdisplay = true; + $CFG->debugsmtp = true; + $CFG->debug = 15; + + // Send test email. + ob_start(); + $success = email_to_user($emailuser, $USER, $subject, $messagetext); + $smtplog = ob_get_contents(); + ob_end_clean(); + + // Restore Moodle debugging options. + $CFG->debug = $debuglevel; + $CFG->debugdisplay = $debugdisplay; + $CFG->debugsmtp = $debugsmtp; + + if ($success) { + $msgparams = new stdClass(); + $msgparams->fromemail = $USER->email; + $msgparams->toemail = $emailuser->email; + $msg = get_string('testoutgoingmailconf_sentmail', 'admin', $msgparams); + $notificationtype = 'notifysuccess'; + } else { + $notificationtype = 'notifyproblem'; + // No communication between Moodle and the SMTP server - no error output. + if (trim($smtplog) == false) { + $msg = get_string('testoutgoingmailconf_errorcommunications', 'admin'); + } else { + $msg = $smtplog; + } + } + + // Show result. + echo $OUTPUT->notification($msg, $notificationtype); +} + +$form->display(); +echo $OUTPUT->footer(); diff --git a/lang/en/admin.php b/lang/en/admin.php index 36c0c7ea9fc..1ea4f7daa84 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -1250,6 +1250,14 @@ $string['taskstatscron'] = 'Background processing for statistics'; $string['tasktagcron'] = 'Background processing for tags'; $string['tasktempfilecleanup'] = 'Delete stale temp files'; $string['tempdatafoldercleanup'] = 'Clean up temporary data files older than'; +$string['testoutgoingmailconf'] = 'Test outgoing mail configuration'; +$string['testoutgoingmaildetail'] = 'Before testing you have to save the configuration.
{$a}'; +$string['testoutgoingmailconf_message'] = 'This is a test message. Please disregard. If you received this email, it means that you have successfully configured your Moodle site\'s email settings.'; +$string['testoutgoingmailconf_errorcommunications'] = 'Moodle could not communicate with your mail server. Start by checking your Moodle Outgoing mail configuration.'; +$string['testoutgoingmailconf_sendtest'] = 'Send a test message'; +$string['testoutgoingmailconf_sentmail'] = 'Moodle successfully delivered the test message to the mail server.
From: {$a->fromemail}
To: {$a->toemail}'; +$string['testoutgoingmailconf_subject'] = '{$a}: test message'; +$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.'; $string['themelist'] = 'Theme list';