[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/message/output/email/ -> message_output_email.php (source)

   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;
  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          $result = email_to_user($recipient, $eventdata->userfrom, $eventdata->subject, $eventdata->fullmessage,
  94                                  $eventdata->fullmessagehtml, $attachment, $attachname, true, $replyto, $replytoname);
  95  
  96          // Remove an attachment file if any.
  97          if (!empty($attachment) && file_exists($attachment)) {
  98              unlink($attachment);
  99          }
 100  
 101          return $result;
 102      }
 103  
 104      /**
 105       * Creates necessary fields in the messaging config form.
 106       *
 107       * @param array $preferences An array of user preferences
 108       */
 109      function config_form($preferences){
 110          global $USER, $OUTPUT, $CFG;
 111          $string = '';
 112  
 113          $choices = array();
 114          $choices['0'] = get_string('textformat');
 115          $choices['1'] = get_string('htmlformat');
 116          $current = $preferences->mailformat;
 117          $string .= $OUTPUT->container(html_writer::label(get_string('emailformat'), 'mailformat'));
 118          $string .= $OUTPUT->container(html_writer::select($choices, 'mailformat', $current, false, array('id' => 'mailformat')));
 119  
 120          if (!empty($CFG->allowusermailcharset)) {
 121              $choices = array();
 122              $charsets = get_list_of_charsets();
 123              if (!empty($CFG->sitemailcharset)) {
 124                  $choices['0'] = get_string('site').' ('.$CFG->sitemailcharset.')';
 125              } else {
 126                  $choices['0'] = get_string('site').' (UTF-8)';
 127              }
 128              $choices = array_merge($choices, $charsets);
 129              $current = $preferences->mailcharset;
 130              $string .= $OUTPUT->container(html_writer::label(get_string('emailcharset'), 'mailcharset'));
 131              $string .= $OUTPUT->container(
 132                  html_writer::select($choices, 'preference_mailcharset', $current, false, array('id' => 'mailcharset'))
 133              );
 134          }
 135  
 136          if (!empty($CFG->messagingallowemailoverride)) {
 137              $inputattributes = array('size' => '30', 'name' => 'email_email', 'value' => $preferences->email_email,
 138                      'id' => 'email_email');
 139              $string .= html_writer::label(get_string('email', 'message_email'), 'email_email');
 140              $string .= $OUTPUT->container(html_writer::empty_tag('input', $inputattributes));
 141  
 142              if (empty($preferences->email_email) && !empty($preferences->userdefaultemail)) {
 143                  $string .= $OUTPUT->container(get_string('ifemailleftempty', 'message_email', $preferences->userdefaultemail));
 144              }
 145  
 146              if (!empty($preferences->email_email) && !validate_email($preferences->email_email)) {
 147                  $string .= $OUTPUT->container(get_string('invalidemail'), 'error');
 148              }
 149  
 150              $string .= '<br/>';
 151          }
 152  
 153          return $string;
 154      }
 155  
 156      /**
 157       * Parses the submitted form data and saves it into preferences array.
 158       *
 159       * @param stdClass $form preferences form class
 160       * @param array $preferences preferences array
 161       */
 162      function process_form($form, &$preferences){
 163          if (isset($form->email_email)) {
 164              $preferences['message_processor_email_email'] = $form->email_email;
 165          }
 166          if (isset($form->preference_mailcharset)) {
 167              $preferences['mailcharset'] = $form->preference_mailcharset;
 168          }
 169      }
 170  
 171      /**
 172       * Returns the default message output settings for this output
 173       *
 174       * @return int The default settings
 175       */
 176      public function get_default_messaging_settings() {
 177          return MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF;
 178      }
 179  
 180      /**
 181       * Loads the config data from database to put on the form during initial form display
 182       *
 183       * @param array $preferences preferences array
 184       * @param int $userid the user id
 185       */
 186      function load_data(&$preferences, $userid){
 187          $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
 188      }
 189  
 190      /**
 191       * Returns true as message can be sent to internal support user.
 192       *
 193       * @return bool
 194       */
 195      public function can_send_to_any_users() {
 196          return true;
 197      }
 198  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1