commit | author | age
|
4e17e6
|
1 |
<?php |
T |
2 |
|
|
3 |
/* |
|
4 |
+-----------------------------------------------------------------------+ |
|
5 |
| program/steps/mail/sendmail.inc | |
|
6 |
| | |
|
7 |
| This file is part of the RoundCube Webmail client | |
|
8 |
| Copyright (C) 2005, RoundCube Dev. - Switzerland | |
30233b
|
9 |
| Licensed under the GNU GPL | |
4e17e6
|
10 |
| | |
T |
11 |
| PURPOSE: | |
|
12 |
| Compose a new mail message with all headers and attachments | |
|
13 |
| and send it using IlohaMail's SMTP methods or with PHP mail() | |
|
14 |
| | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Thomas Bruederli <roundcube@gmail.com> | |
|
17 |
+-----------------------------------------------------------------------+ |
|
18 |
|
|
19 |
$Id$ |
|
20 |
|
|
21 |
*/ |
|
22 |
|
|
23 |
|
968bdc
|
24 |
//require_once('lib/smtp.inc'); |
T |
25 |
require_once('include/rcube_smtp.inc'); |
4e17e6
|
26 |
require_once('Mail/mime.php'); |
T |
27 |
|
|
28 |
|
|
29 |
if (!isset($_SESSION['compose']['id'])) |
|
30 |
{ |
10a699
|
31 |
rcmail_overwrite_action('list'); |
4e17e6
|
32 |
return; |
T |
33 |
} |
|
34 |
|
|
35 |
|
|
36 |
/****** message sending functions ********/ |
|
37 |
|
|
38 |
|
|
39 |
function rcmail_get_identity($id) |
|
40 |
{ |
13c1af
|
41 |
global $DB, $CHARSET, $OUTPUT; |
4e17e6
|
42 |
|
T |
43 |
// get identity record |
d7cb77
|
44 |
$sql_result = $DB->query("SELECT *, email AS mailto |
S |
45 |
FROM ".get_table_name('identities')." |
|
46 |
WHERE identity_id=? |
|
47 |
AND user_id=? |
1cded8
|
48 |
AND del<>1", |
d7cb77
|
49 |
$id,$_SESSION['user_id']); |
4e17e6
|
50 |
|
T |
51 |
if ($DB->num_rows($sql_result)) |
|
52 |
{ |
|
53 |
$sql_arr = $DB->fetch_assoc($sql_result); |
|
54 |
$out = $sql_arr; |
abb32e
|
55 |
$name = strpos($sql_arr['name'], ",") ? '"'.$sql_arr['name'].'"' : $sql_arr['name']; |
13c1af
|
56 |
$out['string'] = sprintf('%s <%s>', |
abb32e
|
57 |
rcube_charset_convert($name, $CHARSET, $OUTPUT->get_charset()), |
13c1af
|
58 |
$sql_arr['mailto']); |
4e17e6
|
59 |
return $out; |
T |
60 |
} |
|
61 |
|
|
62 |
return FALSE; |
|
63 |
} |
|
64 |
|
41fa0b
|
65 |
|
b068a0
|
66 |
if (strlen($_POST['_draft_saveid']) > 3) |
T |
67 |
$olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST); |
|
68 |
|
f0f98f
|
69 |
$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.rand(),true)), $_SESSION['imap_host']); |
b068a0
|
70 |
$savedraft = !empty($_POST['_draft']) ? TRUE : FALSE; |
41fa0b
|
71 |
|
T |
72 |
// remove all scripts and act as called in frame |
|
73 |
$OUTPUT->reset(); |
|
74 |
$_framed = TRUE; |
|
75 |
|
b068a0
|
76 |
|
4e17e6
|
77 |
/****** check submission and compose message ********/ |
10a699
|
78 |
|
T |
79 |
|
|
80 |
if (empty($_POST['_to']) && empty($_POST['_subject']) && $_POST['_message']) |
|
81 |
{ |
|
82 |
show_message("sendingfailed", 'error'); |
41fa0b
|
83 |
//rcmail_overwrite_action('compose'); |
T |
84 |
rcube_iframe_response(); |
10a699
|
85 |
return; |
T |
86 |
} |
4e17e6
|
87 |
|
T |
88 |
|
5bc8cb
|
89 |
// set default charset |
13c1af
|
90 |
$input_charset = $OUTPUT->get_charset(); |
c03095
|
91 |
$message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset; |
T |
92 |
|
1cded8
|
93 |
$mailto_regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m'); |
T |
94 |
$mailto_replace = array(', ', ', ', ''); |
4e17e6
|
95 |
|
abb32e
|
96 |
// replace new lines and strip ending ', ' |
ea7c46
|
97 |
$mailto = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
98 |
|
T |
99 |
// decode address strings |
|
100 |
$to_address_arr = $IMAP->decode_address_list($mailto); |
b068a0
|
101 |
$identity_arr = rcmail_get_identity(get_input_value('_from', RCUBE_INPUT_POST)); |
4e17e6
|
102 |
|
T |
103 |
$from = $identity_arr['mailto']; |
|
104 |
$first_to = is_array($to_address_arr[0]) ? $to_address_arr[0]['mailto'] : $mailto; |
|
105 |
|
b068a0
|
106 |
if (empty($identity_arr['string'])) |
T |
107 |
$identity_arr['string'] = $from; |
4e17e6
|
108 |
|
T |
109 |
// compose headers array |
|
110 |
$headers = array('Date' => date('D, j M Y G:i:s O'), |
|
111 |
'From' => $identity_arr['string'], |
508442
|
112 |
'To' => rcube_charset_convert($mailto, $input_charset, $message_charset)); |
4e17e6
|
113 |
|
T |
114 |
// additional recipients |
ea7c46
|
115 |
if (!empty($_POST['_cc'])) |
T |
116 |
$headers['Cc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
117 |
|
ea7c46
|
118 |
if (!empty($_POST['_bcc'])) |
T |
119 |
$headers['Bcc'] = preg_replace($mailto_regexp, $mailto_replace, get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset)); |
4e17e6
|
120 |
|
ea7c46
|
121 |
if (!empty($identity_arr['bcc'])) |
4e17e6
|
122 |
$headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc']; |
T |
123 |
|
|
124 |
// add subject |
ea7c46
|
125 |
$headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, FALSE, $message_charset)); |
4e17e6
|
126 |
|
ea7c46
|
127 |
if (!empty($identity_arr['organization'])) |
4e17e6
|
128 |
$headers['Organization'] = $identity_arr['organization']; |
T |
129 |
|
ea7c46
|
130 |
if (!empty($identity_arr['reply-to'])) |
4e17e6
|
131 |
$headers['Reply-To'] = $identity_arr['reply-to']; |
T |
132 |
|
f88d41
|
133 |
if (!empty($_SESSION['compose']['reply_msgid'])) |
4e17e6
|
134 |
$headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid']; |
T |
135 |
|
f88d41
|
136 |
if (!empty($_SESSION['compose']['references'])) |
T |
137 |
$headers['References'] = $_SESSION['compose']['references']; |
4e17e6
|
138 |
|
ea7c46
|
139 |
if (!empty($_POST['_priority'])) |
4e17e6
|
140 |
{ |
T |
141 |
$priority = (int)$_POST['_priority']; |
|
142 |
$a_priorities = array(1=>'lowest', 2=>'low', 4=>'high', 5=>'highest'); |
|
143 |
if ($str_priority = $a_priorities[$priority]) |
|
144 |
$headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority)); |
|
145 |
} |
|
146 |
|
620439
|
147 |
if (!empty($_POST['_receipt'])) |
T |
148 |
{ |
|
149 |
$headers['Return-Receipt-To'] = $identity_arr['string']; |
|
150 |
$headers['Disposition-Notification-To'] = $identity_arr['string']; |
|
151 |
} |
4e17e6
|
152 |
|
T |
153 |
// additional headers |
|
154 |
$headers['Message-ID'] = $message_id; |
|
155 |
$headers['X-Sender'] = $from; |
|
156 |
|
ea7c46
|
157 |
if (!empty($CONFIG['useragent'])) |
4e17e6
|
158 |
$headers['User-Agent'] = $CONFIG['useragent']; |
T |
159 |
|
c03095
|
160 |
// fetch message body |
ea7c46
|
161 |
$message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset); |
4e17e6
|
162 |
|
4b0f65
|
163 |
// append generic footer to all messages |
T |
164 |
if (!empty($CONFIG['generic_message_footer'])) |
|
165 |
{ |
|
166 |
$file = realpath($CONFIG['generic_message_footer']); |
|
167 |
if($fp = fopen($file, 'r')) |
|
168 |
{ |
|
169 |
$content = fread($fp, filesize($file)); |
|
170 |
fclose($fp); |
c03095
|
171 |
$message_body .= "\r\n" . rcube_charset_convert($content, 'UTF-8', $message_charset); |
4b0f65
|
172 |
} |
T |
173 |
} |
|
174 |
|
4647e1
|
175 |
// try to autodetect operating system and use the correct line endings |
4b0f65
|
176 |
// use the configured delimiter for headers |
4647e1
|
177 |
if (!empty($CONFIG['mail_header_delimiter'])) |
T |
178 |
$header_delm = $CONFIG['mail_header_delimiter']; |
|
179 |
else if (strtolower(substr(PHP_OS, 0, 3)=='win')) |
|
180 |
$header_delm = "\r\n"; |
|
181 |
else if (strtolower(substr(PHP_OS, 0, 3)=='mac')) |
|
182 |
$header_delm = "\r\n"; |
|
183 |
else |
|
184 |
$header_delm = "\n"; |
4b0f65
|
185 |
|
4e17e6
|
186 |
// create PEAR::Mail_mime instance |
4b0f65
|
187 |
$MAIL_MIME = new Mail_mime($header_delm); |
c03095
|
188 |
$MAIL_MIME->setTXTBody($message_body, FALSE, TRUE); |
T |
189 |
//$MAIL_MIME->setTXTBody(wordwrap($message_body), FALSE, TRUE); |
4e17e6
|
190 |
|
T |
191 |
|
|
192 |
// add stored attachments, if any |
|
193 |
if (is_array($_SESSION['compose']['attachments'])) |
|
194 |
foreach ($_SESSION['compose']['attachments'] as $attachment) |
|
195 |
$MAIL_MIME->addAttachment($attachment['path'], $attachment['mimetype'], $attachment['name'], TRUE); |
|
196 |
|
|
197 |
|
|
198 |
// add submitted attachments |
|
199 |
if (is_array($_FILES['_attachments']['tmp_name'])) |
|
200 |
foreach ($_FILES['_attachments']['tmp_name'] as $i => $filepath) |
|
201 |
$MAIL_MIME->addAttachment($filepath, $files['type'][$i], $files['name'][$i], TRUE); |
|
202 |
|
1cded8
|
203 |
|
f88d41
|
204 |
// chose transfer encoding |
T |
205 |
$charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15'); |
|
206 |
$transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit'; |
|
207 |
|
a95e0e
|
208 |
// encoding settings for mail composing |
f88d41
|
209 |
$message_param = array('text_encoding' => $transfer_encoding, |
a95e0e
|
210 |
'html_encoding' => 'quoted-printable', |
T |
211 |
'head_encoding' => 'quoted-printable', |
1cded8
|
212 |
'head_charset' => $message_charset, |
T |
213 |
'html_charset' => $message_charset, |
|
214 |
'text_charset' => $message_charset); |
4e17e6
|
215 |
|
T |
216 |
// compose message body and get headers |
b068a0
|
217 |
$msg_body = &$MAIL_MIME->get($message_param); |
4e17e6
|
218 |
|
f88d41
|
219 |
$msg_subject = $headers['Subject']; |
13c1af
|
220 |
|
T |
221 |
if ($MBSTRING && function_exists("mb_encode_mimeheader")) |
|
222 |
$headers['Subject'] = mb_encode_mimeheader($headers['Subject'], $message_charset); |
4e17e6
|
223 |
|
1966c5
|
224 |
// Begin SMTP Delivery Block |
S |
225 |
if (!$savedraft) { |
4e17e6
|
226 |
|
1966c5
|
227 |
// send thru SMTP server using custom SMTP library |
S |
228 |
if ($CONFIG['smtp_server']) |
|
229 |
{ |
|
230 |
// generate list of recipients |
|
231 |
$a_recipients = array($mailto); |
|
232 |
|
|
233 |
if (strlen($headers['Cc'])) |
|
234 |
$a_recipients[] = $headers['Cc']; |
|
235 |
if (strlen($headers['Bcc'])) |
|
236 |
$a_recipients[] = $headers['Bcc']; |
|
237 |
|
|
238 |
// clean Bcc from header for recipients |
|
239 |
$send_headers = $headers; |
|
240 |
unset($send_headers['Bcc']); |
|
241 |
|
|
242 |
// generate message headers |
|
243 |
$header_str = $MAIL_MIME->txtHeaders($send_headers); |
|
244 |
|
|
245 |
// send message |
|
246 |
$sent = smtp_mail($from, $a_recipients, $header_str, $msg_body); |
|
247 |
|
|
248 |
// log error |
|
249 |
if (!$sent) |
|
250 |
{ |
|
251 |
raise_error(array('code' => 800, |
|
252 |
'type' => 'smtp', |
|
253 |
'line' => __LINE__, |
|
254 |
'file' => __FILE__, |
|
255 |
'message' => "SMTP error: $SMTP_ERROR"), TRUE, FALSE); |
|
256 |
} |
|
257 |
} |
|
258 |
|
|
259 |
// send mail using PHP's mail() function |
|
260 |
else |
|
261 |
{ |
|
262 |
// unset some headers because they will be added by the mail() function |
|
263 |
$headers_enc = $MAIL_MIME->headers($headers); |
|
264 |
$headers_php = $MAIL_MIME->_headers; |
|
265 |
unset($headers_php['To'], $headers_php['Subject']); |
|
266 |
|
|
267 |
// reset stored headers and overwrite |
|
268 |
$MAIL_MIME->_headers = array(); |
|
269 |
$header_str = $MAIL_MIME->txtHeaders($headers_php); |
|
270 |
|
|
271 |
if (ini_get('safe_mode')) |
|
272 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str); |
|
273 |
else |
|
274 |
$sent = mail($headers_enc['To'], $headers_enc['Subject'], $msg_body, $header_str, "-f$from"); |
|
275 |
} |
|
276 |
|
|
277 |
|
|
278 |
// return to compose page if sending failed |
968bdc
|
279 |
if (!$sent) |
4e17e6
|
280 |
{ |
1966c5
|
281 |
show_message("sendingfailed", 'error'); |
41fa0b
|
282 |
rcube_iframe_response(); |
1966c5
|
283 |
return; |
4e17e6
|
284 |
} |
13c1af
|
285 |
|
1966c5
|
286 |
|
S |
287 |
// set repliead flag |
|
288 |
if ($_SESSION['compose']['reply_uid']) |
|
289 |
$IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED'); |
c03095
|
290 |
|
1966c5
|
291 |
} // End of SMTP Delivery Block |
41fa0b
|
292 |
|
T |
293 |
|
4e17e6
|
294 |
|
1966c5
|
295 |
// Determine which folder to save message |
b068a0
|
296 |
if ($savedraft) |
T |
297 |
$store_target = 'drafts_mbox'; |
|
298 |
else |
|
299 |
$store_target = 'sent_mbox'; |
c03095
|
300 |
|
1966c5
|
301 |
if ($CONFIG[$store_target]) |
4e17e6
|
302 |
{ |
T |
303 |
// create string of complete message headers |
|
304 |
$header_str = $MAIL_MIME->txtHeaders($headers); |
|
305 |
|
|
306 |
// check if mailbox exists |
1966c5
|
307 |
if (!in_array_nocase($CONFIG[$store_target], $IMAP->list_mailboxes())) |
S |
308 |
$store_folder = $IMAP->create_mailbox($CONFIG[$store_target], TRUE); |
520c36
|
309 |
else |
1966c5
|
310 |
$store_folder = TRUE; |
b068a0
|
311 |
|
T |
312 |
// add headers to message body |
|
313 |
$msg_body = $header_str."\r\n".$msg_body; |
4e17e6
|
314 |
|
T |
315 |
// append message to sent box |
1966c5
|
316 |
if ($store_folder) |
b068a0
|
317 |
$saved = $IMAP->save_message($CONFIG[$store_target], $msg_body); |
520c36
|
318 |
|
T |
319 |
// raise error if saving failed |
|
320 |
if (!$saved) |
f0f98f
|
321 |
{ |
520c36
|
322 |
raise_error(array('code' => 800, |
T |
323 |
'type' => 'imap', |
|
324 |
'file' => __FILE__, |
1966c5
|
325 |
'message' => "Could not save message in $CONFIG[$store_target]"), TRUE, FALSE); |
41fa0b
|
326 |
|
T |
327 |
show_message('errorsaving', 'error'); |
|
328 |
rcube_iframe_response($errorout); |
f0f98f
|
329 |
} |
4e17e6
|
330 |
|
b068a0
|
331 |
if ($olddraftmessageid) |
T |
332 |
{ |
1966c5
|
333 |
// delete previous saved draft |
S |
334 |
$a_deleteid = $IMAP->search($CONFIG['drafts_mbox'],'HEADER Message-ID',$olddraftmessageid); |
|
335 |
$deleted = $IMAP->delete_message($IMAP->get_uid($a_deleteid[0],$CONFIG['drafts_mbox']),$CONFIG['drafts_mbox']); |
4e17e6
|
336 |
|
f0f98f
|
337 |
// raise error if deletion of old draft failed |
1966c5
|
338 |
if (!$deleted) |
S |
339 |
raise_error(array('code' => 800, |
|
340 |
'type' => 'imap', |
|
341 |
'file' => __FILE__, |
|
342 |
'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE); |
4e17e6
|
343 |
} |
T |
344 |
} |
|
345 |
|
1966c5
|
346 |
if ($savedraft) |
S |
347 |
{ |
f0f98f
|
348 |
// clear the "saving message" busy status, and display success |
41fa0b
|
349 |
show_message('messagesaved', 'confirmation'); |
f0f98f
|
350 |
|
S |
351 |
// update "_draft_saveid" on the page, which is used to delete a previous draft |
41fa0b
|
352 |
$frameout = "var foundid = parent.rcube_find_object('_draft_saveid', parent.document);\n"; |
T |
353 |
$frameout .= sprintf("foundid.value = '%s';\n", str_replace(array('<','>'), "", $message_id)); |
f0f98f
|
354 |
|
S |
355 |
// update the "cmp_hash" to prevent "Unsaved changes" warning |
41fa0b
|
356 |
$frameout .= sprintf("parent.%s.cmp_hash = parent.%s.compose_field_hash();\n", $JS_OBJECT_NAME, $JS_OBJECT_NAME); |
T |
357 |
|
f0f98f
|
358 |
// start the auto-save timer again |
41fa0b
|
359 |
$frameout .= sprintf("parent.%s.auto_save_start();", $JS_OBJECT_NAME); |
f0f98f
|
360 |
|
S |
361 |
// send html page with JS calls as response |
41fa0b
|
362 |
rcube_iframe_response($frameout); |
1966c5
|
363 |
} |
S |
364 |
else |
|
365 |
{ |
|
366 |
if ($CONFIG['smtp_log']) |
|
367 |
{ |
|
368 |
$log_entry = sprintf("[%s] User: %d on %s; Message for %s; Subject: %s\n", |
|
369 |
date("d-M-Y H:i:s O", mktime()), |
|
370 |
$_SESSION['user_id'], |
|
371 |
$_SERVER['REMOTE_ADDR'], |
|
372 |
$mailto, |
|
373 |
$msg_subject); |
4e17e6
|
374 |
|
1966c5
|
375 |
if ($fp = @fopen($CONFIG['log_dir'].'/sendmail', 'a')) |
S |
376 |
{ |
|
377 |
fwrite($fp, $log_entry); |
|
378 |
fclose($fp); |
|
379 |
} |
|
380 |
} |
41fa0b
|
381 |
|
1966c5
|
382 |
rcmail_compose_cleanup(); |
41fa0b
|
383 |
rcube_iframe_response(sprintf("parent.$JS_OBJECT_NAME.sent_successfully('%s');", |
T |
384 |
rep_specialchars_output(rcube_label('messagesent'), 'js'))); |
1966c5
|
385 |
} |
4e17e6
|
386 |
|
T |
387 |
|
1966c5
|
388 |
?> |