message MDL-24548 some emergency bug fixes to messaging
[moodle.git] / message / output / email / message_output_email.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // NOTICE OF COPYRIGHT                                                   //
6 //                                                                       //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
8 //          http://moodle.com                                            //
9 //                                                                       //
10 // Copyright (C) 1999 onwards  Martin Dougiamas  http://moodle.com       //
11 //                                                                       //
12 // This program is free software; you can redistribute it and/or modify  //
13 // it under the terms of the GNU General Public License as published by  //
14 // the Free Software Foundation; either version 2 of the License, or     //
15 // (at your option) any later version.                                   //
16 //                                                                       //
17 // This program is distributed in the hope that it will be useful,       //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
20 // GNU General Public License for more details:                          //
21 //                                                                       //
22 //          http://www.gnu.org/copyleft/gpl.html                         //
23 //                                                                       //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27  * Email message processor - send a given message by email
28  *
29  * @author Luis Rodrigues
30  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31  * @package
32  */
33 require_once($CFG->dirroot.'/message/output/lib.php');
35 class message_output_email extends message_output {
36     /**
37      * Processes the message (sends by email).
38      * @param object $message the message to be sent
39      */
40     function send_message($message) {
41         global $DB, $SITE;
43         $userto = $DB->get_record('user', array('id' => $message->useridto));
44         $userfrom = $DB->get_record('user', array('id' => $message->useridfrom));
46         //check user preference for where user wants email sent
47         $usertoemailaddress = get_user_preferences('message_processor_email_email', '', $message->useridto);
49         if ( !empty($usertoemailaddress)) {
50             $userto->email = $usertoemailaddress;
51         }
53         //concatenating the footer on here so that it appears on emails but not within the saved message
54         $messagetosend = null;
55         if (!empty($message->fullmessage)) {
56             $messagetosend = $message->fullmessage.$message->footer;
57         }
59         $messagetosendhtml = null;
60         if (!empty($message->fullmessagehtml)) {
61             $messagetosendhtml = $message->fullmessagehtml.$message->footerhtml;
62         }
64         $result = email_to_user($userto, $userfrom,
65             $message->subject, $messagetosend, $messagetosendhtml);
67         return $result===true; //email_to_user() can return true, false or "emailstop"
68         //return true;//do we want to report an error if email sending fails?
69     }
71     /**
72      * Creates necessary fields in the messaging config form.
73      * @param object $mform preferences form class
74      */
75     function config_form($preferences){
76         global $USER;
77         $string = get_string('email').': <input size="30" name="email_email" value="'.$preferences->email_email.'" />';
78         if (empty($preferences->email_email)) {
79             $string .= ' ('.get_string('default').': '.$USER->email.')';
80         }
81         return $string;
82     }
84     /**
85      * Parses the form submitted data and saves it into preferences array.
86      * @param object $mform preferences form class
87      * @param array $preferences preferences array
88      */
89     function process_form($form, &$preferences){
90         $preferences['message_processor_email_email'] = $form->email_email;
91     }
93     /**
94      * Loads the config data from database to put on the form (initial load)
95      * @param array $preferences preferences array
96      * @param int $userid the user id
97      */
98     function load_data(&$preferences, $userid){
99         $preferences->email_email = get_user_preferences( 'message_processor_email_email', '', $userid);
100     }