on-demand release 4.0dev+
[moodle.git] / message / output / email / message_output_email.php
CommitLineData
3b120e46 1<?php
6fbd60ef
AD
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/>.
3b120e46 16
17/**
6fbd60ef 18 * Contains the definiton of the email message processors (sends messages to users via email)
3b120e46 19 *
6fbd60ef
AD
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
3b120e46 23 */
6fbd60ef 24
3b120e46 25require_once($CFG->dirroot.'/message/output/lib.php');
26
6fbd60ef
AD
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 */
3b120e46 34class message_output_email extends message_output {
35 /**
36 * Processes the message (sends by email).
fe983847 37 * @param object $eventdata the event data submitted by the message sender plus $eventdata->savedmessageid
3b120e46 38 */
fe983847 39 function send_message($eventdata) {
9b6034bc 40 global $CFG, $DB;
38d93cb0 41
ca8fe0b0
PS
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
d3a81842
AD
47 //the user the email is going to
48 $recipient = null;
62f7ce79 49
d3a81842
AD
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);
9a3e8a1f 52 $emailmessagingpreference = clean_param($emailmessagingpreference, PARAM_EMAIL);
84f533b3
AD
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)) {
d3a81842
AD
57 //clone to avoid altering the actual user object
58 $recipient = clone($eventdata->userto);
59 $recipient->email = $emailmessagingpreference;
60 } else {
61 $recipient = $eventdata->userto;
62f7ce79 62 }
56b9a02e
YB
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();
9a2c5257 77
56b9a02e
YB
78 // Get attachment file name.
79 $attachname = clean_filename($eventdata->attachname);
80 }
81 }
82
6c0bfb14
AN
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
9b6034bc
MN
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 }
56b9a02e
YB
112
113 // Remove an attachment file if any.
9a2c5257
SL
114 if (!empty($attachment) && file_exists($attachment)) {
115 unlink($attachment);
68e50bc8 116 }
3b120e46 117
46505ee7 118 return $result;
3b120e46 119 }
120
1d422980 121 /**
3b120e46 122 * Creates necessary fields in the messaging config form.
6fbd60ef
AD
123 *
124 * @param array $preferences An array of user preferences
3b120e46 125 */
1d422980 126 function config_form($preferences){
84f533b3 127 global $USER, $OUTPUT, $CFG;
479fa47d
DW
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')));
a0eabdd3 136 $string .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'userid', 'value' => $USER->id));
479fa47d
DW
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 );
84f533b3 152 }
dbd634ff 153
479fa47d 154 if (!empty($CFG->messagingallowemailoverride)) {
45367bdf
AG
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');
479fa47d 158 $string .= $OUTPUT->container(html_writer::empty_tag('input', $inputattributes));
0d447fe6 159
479fa47d
DW
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 }
dbd634ff 167
479fa47d 168 $string .= '<br/>';
dbd634ff
AD
169 }
170
305a014f 171 return $string;
3b120e46 172 }
173
174 /**
6fbd60ef
AD
175 * Parses the submitted form data and saves it into preferences array.
176 *
177 * @param stdClass $form preferences form class
1d422980 178 * @param array $preferences preferences array
3b120e46 179 */
d18b1bbd 180 function process_form($form, &$preferences){
a0eabdd3
RW
181 global $CFG;
182
492b00b7 183 if (isset($form->email_email)) {
6e65554e 184 $preferences['message_processor_email_email'] = clean_param($form->email_email, PARAM_EMAIL);
6c0f13a5 185 }
479fa47d
DW
186 if (isset($form->preference_mailcharset)) {
187 $preferences['mailcharset'] = $form->preference_mailcharset;
6e65554e
MG
188 if (!array_key_exists($preferences['mailcharset'], get_list_of_charsets())) {
189 $preferences['mailcharset'] = '0';
190 }
479fa47d 191 }
a0eabdd3
RW
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 }
3b120e46 199 }
200
7529f9e9 201 /**
6fbd60ef
AD
202 * Returns the default message output settings for this output
203 *
204 * @return int The default settings
7529f9e9
TH
205 */
206 public function get_default_messaging_settings() {
207 return MESSAGE_PERMITTED + MESSAGE_DEFAULT_LOGGEDIN + MESSAGE_DEFAULT_LOGGEDOFF;
208 }
209
3b120e46 210 /**
6fbd60ef
AD
211 * Loads the config data from database to put on the form during initial form display
212 *
1d422980 213 * @param array $preferences preferences array
3b120e46 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 }
3bcf6b3c
RT
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 }
3b120e46 228}