Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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   * Contains the definiton of the email message processors (sends messages to users via email)
  19   *
  20   * @package   message_email
  21   * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once($CFG->dirroot.'/message/output/lib.php');
  26  
  27  /**
  28   * The email message processor
  29   *
  30   * @package   message_email
  31   * @copyright 2008 Luis Rodrigues and Martin Dougiamas
  32   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class message_output_email extends message_output {
  35      /**
  36       * Processes the message (sends by email).
  37       * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
  38       */
  39      function send_message($eventdata) {
  40          global $CFG, $DB;
  41  
  42          // skip any messaging suspended and deleted users
  43          if ($eventdata->userto->auth === 'nologin' or $eventdata->userto->suspended or $eventdata->userto->deleted) {
  44              return true;
  45          }
  46  
  47          //the user the email is going to
  48          $recipient = null;
  49  
  50          //check if the recipient has a different email address specified in their messaging preferences Vs their user profile
  51          $emailmessagingpreference = get_user_preferences('message_processor_email_email', null, $eventdata->userto);
  52          $emailmessagingpreference = clean_param($emailmessagingpreference, PARAM_EMAIL);
  53  
  54          // If the recipient has set an email address in their preferences use that instead of the one in their profile
  55          // but only if overriding the notification email address is allowed
  56          if (!empty($emailmessagingpreference) && !empty($CFG->messagingallowemailoverride)) {
  57              //clone to avoid altering the actual user object
  58              $recipient = clone($eventdata->userto);
  59              $recipient->email = $emailmessagingpreference;
  60          } else {
  61              $recipient = $eventdata->userto;
  62          }
  63  
  64          // Check if we have attachments to send.
  65          $attachment = '';
  66          $attachname = '';
  67          if (!empty($CFG->allowattachments) && !empty($eventdata->attachment)) {
  68              if (empty($eventdata->attachname)) {
  69                  // Attachment needs a file name.
  70                  debugging('Attachments should have a file name. No attachments have been sent.', DEBUG_DEVELOPER);
  71              } else if (!($eventdata->attachment instanceof stored_file)) {
  72                  // Attachment should be of a type stored_file.
  73                  debugging('Attachments should be of type stored_file. No attachments have been sent.', DEBUG_DEVELOPER);
  74              } else {
  75                  // Copy attachment file to a temporary directory and get the file path.
  76                  $attachment = $eventdata->attachment->copy_content_to_temp();
  77  
  78                  // Get attachment file name.
  79                  $attachname = clean_filename($eventdata->attachname);
  80              }
  81          }
  82  
  83          // Configure mail replies - this is used for incoming mail replies.
  84          $replyto = '';
  85          $replytoname = '';
  86          if (isset($eventdata->replyto)) {
  87              $replyto = $eventdata->replyto;
  88              if (isset($eventdata->replytoname)) {
  89                  $replytoname = $eventdata->replytoname;
  90              }
  91          }
  92  
  93          // We email messages from private conversations straight away, but for group we add them to a table to be sent later.
  94          $emailuser = true;
  95          if (!$eventdata->notification) {
  96              if ($eventdata->conversationtype == \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP) {
  97                  $emailuser = false;
  98              }
  99          }
 100  
 101          if ($emailuser) {
 102              $result = email_to_user($recipient, $eventdata->userfrom, $eventdata->subject, $eventdata->fullmessage,
 103                  $eventdata->fullmessagehtml, $attachment, $attachname, true, $replyto, $replytoname);
 104          } else {
 105              $messagetosend = new stdClass();
 106              $messagetosend->useridfrom = $eventdata->userfrom->id;
 107              $messagetosend->useridto = $recipient->id;
 108              $messagetosend->conversationid = $eventdata->convid;
 109              $messagetosend->messageid = $eventdata->savedmessageid;
 110              $result = $DB->insert_record('message_email_messages', $messagetosend, false);
 111          }
 112  
 113          // Remove an attachment file if any.
 114          if (!empty($attachment) && file_exists($attachment)) {
 115              unlink($attachment);
 116          }
 117  
 118          return $result;
 119      }
 120  
 121      /**
 122       * Creates necessary fields in the messaging config form.
 123       *
 124       * @param array $preferences An array of user preferences
 125       */
 126      function config_form($preferences){
 127          global $USER, $OUTPUT, $CFG;
 128          $string = '';
 129  
 130          $choices = array();
 131          $choices['0'] = get_string('textformat');
 132          $choices['1'] = get_string('htmlformat');
 133          $current = $preferences->mailformat;
 134          $string .= $OUTPUT->container(html_writer::label(get_string('emailformat'), 'mailformat'));
 135          $string .= $OUTPUT->container(html_writer::select($choices, 'mailformat', $current, false, array('id' => 'mailformat')));
 136          $string .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'userid', 'value' => $USER->id));
 137  
 138          if (!empty($CFG->allowusermailcharset)) {
 139              $choices = array();
 140              $charsets = get_list_of_charsets();
 141              if (!empty($CFG->sitemailcharset)) {
 142                  $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
 143              } else {
 144                  $choices['0'] = get_string('site').' (UTF-8)';
 145              }
 146              $choices = array_merge($choices, $charsets);
 147              $current = $preferences->mailcharset;
 148              $string .= $OUTPUT->container(html_writer::label(get_string('emailcharset'), 'mailcharset'));
 149              $string .= $OUTPUT->container(
 150                  html_writer::select($choices, 'preference_mailcharset', $current, false, array('id' => 'mailcharset'))
 151              );
 152          }
 153  
 154          if (!empty($CFG->messagingallowemailoverride)) {
 155              $inputattributes = array('size' => '30', 'name' => 'email_email', 'value' => $preferences->email_email,
 156                      'id' => 'email_email');
 157              $string .= html_writer::label(get_string('email', 'message_email'), 'email_email');
 158              $string .= $OUTPUT->container(html_writer::empty_tag('input', $inputattributes));
 159  
 160              if (empty($preferences->email_email) && !empty($preferences->userdefaultemail)) {
 161                  $string .= $OUTPUT->container(get_string('ifemailleftempty', 'message_email', $preferences->userdefaultemail));
 162              }
 163  
 164              if (!empty($preferences->email_email) && !validate_email($preferences->email_email)) {
 165                  $string .= $OUTPUT->container(get_string('invalidemail'), 'error');
 166              }
 167  
 168              $string .= '<br/>';
 169          }
 170  
 171          return $string;
 172      }
 173  
 174      /**
 175       * Parses the submitted form data and saves it into preferences array.
 176       *
 177       * @param stdClass $form preferences form class
 178       * @param array $preferences preferences array
 179       */
 180      function process_form($form, &$preferences){
 181          global $CFG;
 182  
 183          if (isset($form->email_email)) {
 184              $preferences['message_processor_email_email'] = clean_param($form->email_email, PARAM_EMAIL);
 185          }
 186          if (isset($form->preference_mailcharset)) {
 187              $preferences['mailcharset'] = $form->preference_mailcharset;
 188              if (!array_key_exists($preferences['mailcharset'], get_list_of_charsets())) {
 189                  $preferences['mailcharset'] = '0';
 190              }
 191          }
 192          if (isset($form->mailformat) && isset($form->userid)) {
 193              require_once($CFG->dirroot.'/user/lib.php');
 194  
 195              $user = core_user::get_user($form->userid, '*', MUST_EXIST);
 196              $user->mailformat = clean_param($form->mailformat, PARAM_INT);
 197              user_update_user($user, false, false);
 198          }
 199      }
 200  
 201      /**
 202       * Returns the default message output settings for this output
 203       *
 204       * @return int The default settings
 205       */
 206      public function get_default_messaging_settings() {
 207          return MESSAGE_PERMITTED + MESSAGE_DEFAULT_ENABLED;
 208      }
 209  
 210      /**
 211       * Loads the config data from database to put on the form during initial form display
 212       *
 213       * @param array $preferences preferences array
 214       * @param int $userid the user id
 215       */
 216      function load_data(&$preferences, $userid){
 217          $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
 218      }
 219  
 220      /**
 221       * Returns true as message can be sent to internal support user.
 222       *
 223       * @return bool
 224       */
 225      public function can_send_to_any_users() {
 226          return true;
 227      }
 228  }