Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Test output mail configuration page
  19   *
  20   * @copyright 2019 Victor Deniz <victor@moodle.com>, based on Michael Milette <michael.milette@tngconsulting.ca> code
  21   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  require_once(__DIR__ . '/../config.php');
  25  require_once($CFG->libdir.'/adminlib.php');
  26  
  27  // This is an admin page.
  28  admin_externalpage_setup('testoutgoingmailconf');
  29  
  30  $headingtitle = get_string('testoutgoingmailconf', 'admin');
  31  $homeurl = new moodle_url('/admin/category.php', array('category' => 'email'));
  32  $returnurl = new moodle_url('/admin/testoutgoingconf.php');
  33  
  34  $form = new core_admin\form\testoutgoingmailconf_form(null, ['returnurl' => $returnurl]);
  35  if ($form->is_cancelled()) {
  36      redirect($homeurl);
  37  }
  38  
  39  // Display the page.
  40  echo $OUTPUT->header();
  41  echo $OUTPUT->heading($headingtitle);
  42  
  43  $data = $form->get_data();
  44  if ($data) {
  45      $emailuser = new stdClass();
  46      $emailuser->email = $data->recipient;
  47      $emailuser->id = -99;
  48  
  49      $subject = get_string('testoutgoingmailconf_subject', 'admin',
  50          format_string($SITE->fullname, true, ['context' => context_system::instance()]));
  51      $messagetext = get_string('testoutgoingmailconf_message', 'admin');
  52  
  53      // Manage Moodle debugging options.
  54      $debuglevel = $CFG->debug;
  55      $debugdisplay = $CFG->debugdisplay;
  56      $debugsmtp = $CFG->debugsmtp ?? null; // This might not be set as it's optional.
  57      $CFG->debugdisplay = true;
  58      $CFG->debugsmtp = true;
  59      $CFG->debug = 15;
  60  
  61      // Send test email.
  62      ob_start();
  63      $success = email_to_user($emailuser, $USER, $subject, $messagetext);
  64      $smtplog = ob_get_contents();
  65      ob_end_clean();
  66  
  67      // Restore Moodle debugging options.
  68      $CFG->debug = $debuglevel;
  69      $CFG->debugdisplay = $debugdisplay;
  70  
  71      // Restore the debugsmtp config, if it was set originally.
  72      unset($CFG->debugsmtp);
  73      if (!is_null($debugsmtp)) {
  74          $CFG->debugsmtp = $debugsmtp;
  75      }
  76  
  77      if ($success) {
  78          $msgparams = new stdClass();
  79          $msgparams->fromemail = $USER->email;
  80          $msgparams->toemail = $emailuser->email;
  81          $msg = get_string('testoutgoingmailconf_sentmail', 'admin', $msgparams);
  82          $notificationtype = 'notifysuccess';
  83      } else {
  84          $notificationtype = 'notifyproblem';
  85          // No communication between Moodle and the SMTP server - no error output.
  86          if (trim($smtplog) == false) {
  87              $msg = get_string('testoutgoingmailconf_errorcommunications', 'admin');
  88          } else {
  89              $msg = $smtplog;
  90          }
  91      }
  92  
  93      // Show result.
  94      echo $OUTPUT->notification($msg, $notificationtype);
  95  }
  96  
  97  $form->display();
  98  echo $OUTPUT->footer();