Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 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      // Get the user who will send this email (From:).
  50      $emailuserfrom = $USER;
  51      if ($data->from) {
  52          if (!$userfrom = \core_user::get_user_by_email($data->from)) {
  53              $userfrom = \core_user::get_user_by_username($data->from);
  54          }
  55          if (!$userfrom && validate_email($data->from)) {
  56              $dummyuser = \core_user::get_user(\core_user::NOREPLY_USER);
  57              $dummyuser->id = -1;
  58              $dummyuser->email = $data->from;
  59              $dummyuser->firstname = $data->from;
  60              $emailuserfrom = $dummyuser;
  61          } else if ($userfrom) {
  62              $emailuserfrom = $userfrom;
  63          }
  64      }
  65  
  66      // Get the date the email will be sent.
  67      $timestamp = userdate(time(), get_string('strftimedatetimeaccurate', 'core_langconfig'));
  68  
  69      // Build the email subject.
  70      $subjectparams = new stdClass();
  71      $subjectparams->site = format_string($SITE->fullname, true, ['context' => context_system::instance()]);
  72      if (isset($data->additionalsubject)) {
  73          $subjectparams->additional = format_string($data->additionalsubject);
  74      }
  75      $subjectparams->time = $timestamp;
  76  
  77      $subject = get_string('testoutgoingmailconf_subject', 'admin', $subjectparams);
  78      $messagetext = get_string('testoutgoingmailconf_message', 'admin', $timestamp);
  79  
  80      // Manage Moodle debugging options.
  81      $debuglevel = $CFG->debug;
  82      $debugdisplay = $CFG->debugdisplay;
  83      $debugsmtp = $CFG->debugsmtp ?? null; // This might not be set as it's optional.
  84      $CFG->debugdisplay = true;
  85      $CFG->debugsmtp = true;
  86      $CFG->debug = 15;
  87  
  88      // Send test email.
  89      ob_start();
  90      $success = email_to_user($emailuser, $emailuserfrom, $subject, $messagetext);
  91      $smtplog = ob_get_contents();
  92      ob_end_clean();
  93  
  94      // Restore Moodle debugging options.
  95      $CFG->debug = $debuglevel;
  96      $CFG->debugdisplay = $debugdisplay;
  97  
  98      // Restore the debugsmtp config, if it was set originally.
  99      unset($CFG->debugsmtp);
 100      if (!is_null($debugsmtp)) {
 101          $CFG->debugsmtp = $debugsmtp;
 102      }
 103  
 104      if ($success) {
 105          $msgparams = new stdClass();
 106          $msgparams->fromemail = $emailuserfrom->email;
 107          $msgparams->toemail = $emailuser->email;
 108          $msg = get_string('testoutgoingmailconf_sentmail', 'admin', $msgparams);
 109          $notificationtype = 'notifysuccess';
 110      } else {
 111          $notificationtype = 'notifyproblem';
 112          // No communication between Moodle and the SMTP server - no error output.
 113          if (trim($smtplog) == false) {
 114              $msg = get_string('testoutgoingmailconf_errorcommunications', 'admin');
 115          } else {
 116              $msg = $smtplog;
 117          }
 118      }
 119  
 120      // Show result.
 121      echo $OUTPUT->notification($msg, $notificationtype);
 122  }
 123  
 124  $form->display();
 125  echo $OUTPUT->footer();