Skip to content

Commit

Permalink
MDL-13745 admin: add Output mail configuration test
Browse files Browse the repository at this point in the history
Add a tool for admins to test that the outgoing mail configuration is valid.
  • Loading branch information
vmdef committed Mar 12, 2019
1 parent a713ed3 commit 2bf67a7
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
59 changes: 59 additions & 0 deletions admin/classes/form/testoutgoingmailconf_form.php
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Testing outgoing mail configuration form
*
* @package core
* @copyright 2019 Victor Deniz <victor@moodle.com>
* @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 <victor@moodle.com>
* @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');

}
}
6 changes: 6 additions & 0 deletions admin/settings/server.php
Expand Up @@ -176,6 +176,8 @@

$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
Expand Down Expand Up @@ -326,6 +328,10 @@
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();
Expand Down
92 changes: 92 additions & 0 deletions admin/testoutgoingmailconf.php
@@ -0,0 +1,92 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Test output mail configuration page
*
* @copyright 2019 Victor Deniz <victor@moodle.com>, based on Michael Milette <michael.milette@tngconsulting.ca> 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();
8 changes: 8 additions & 0 deletions lang/en/admin.php
Expand Up @@ -1250,6 +1250,14 @@
$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.<br />{$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.<br />From: {$a->fromemail}<br />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';
Expand Down

0 comments on commit 2bf67a7

Please sign in to comment.