alecpl
2010-12-01 53604a0550f9940584b7e4d4260b96714ae0edbf
commit | author | age
4e17e6 1 <?php
T 2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/mail/sendmail.inc                                       |
6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
A 8  | Copyright (C) 2005-2010, 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         |
e8f8fe 13  |   and send it using the PEAR::Net_SMTP class or with PHP mail()       |
4e17e6 14  |                                                                       |
T 15  +-----------------------------------------------------------------------+
16  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17  +-----------------------------------------------------------------------+
18
19  $Id$
20
21 */
22
0b6c1c 23 // remove all scripts and act as called in frame
T 24 $OUTPUT->reset();
25 $OUTPUT->framed = TRUE;
26
ad334a 27 $savedraft = !empty($_POST['_draft']) ? true : false;
acb08f 28
A 29 /****** checks ********/
0b6c1c 30
T 31 if (!isset($_SESSION['compose']['id'])) {
ae0040 32   raise_error(array('code' => 500, 'type' => 'php',
10eedb 33     'file' => __FILE__, 'line' => __LINE__,
A 34     'message' => "Invalid compose ID"), true, false);
35
ae0040 36   $OUTPUT->show_message('internalerror', 'error');
0b6c1c 37   $OUTPUT->send('iframe');
T 38 }
4e17e6 39
4b60fa 40 if (!$savedraft) {
A 41   if (empty($_POST['_to']) && empty($_POST['_cc']) && empty($_POST['_bcc'])
42     && empty($_POST['_subject']) && $_POST['_message']) {
43     $OUTPUT->show_message('sendingfailed', 'error');
acb08f 44     $OUTPUT->send('iframe');
4b60fa 45   }
A 46
47   if(!empty($CONFIG['sendmail_delay'])) {
48     $wait_sec = time() - intval($CONFIG['sendmail_delay']) - intval($CONFIG['last_message_time']);
49     if($wait_sec < 0) {
50       $OUTPUT->show_message('senttooquickly', 'error', array('sec' => $wait_sec * -1));
51       $OUTPUT->send('iframe');
acb08f 52     }
4b60fa 53   }
acb08f 54 }
A 55
4e17e6 56
T 57 /****** message sending functions ********/
58
2471d3 59 // encrypt parts of the header
A 60 function rcmail_encrypt_header($what)
61 {
62   global $CONFIG, $RCMAIL;
e99991 63   if (!$CONFIG['http_received_header_encrypt']) {
2471d3 64     return $what;
A 65   }
66   return $RCMAIL->encrypt($what);
67 }
68
fba1f5 69 // get identity record
4e17e6 70 function rcmail_get_identity($id)
e99991 71 {
fba1f5 72   global $USER, $OUTPUT;
4e17e6 73   
e99991 74   if ($sql_arr = $USER->get_identity($id)) {
4e17e6 75     $out = $sql_arr;
fba1f5 76     $out['mailto'] = $sql_arr['email'];
e99991 77     $out['string'] = format_email_recipient($sql_arr['email'],
A 78       rcube_charset_convert($sql_arr['name'], RCMAIL_CHARSET, $OUTPUT->get_charset()));
fd51e0 79
4e17e6 80     return $out;
T 81   }
e99991 82
A 83   return FALSE;
84 }
4e17e6 85
a0109c 86 /**
S 87  * go from this:
141eb8 88  * <img src="http[s]://.../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
a0109c 89  *
S 90  * to this:
91  *
141eb8 92  * <img src="/path/on/server/.../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
a0109c 93  * ...
S 94  */
141eb8 95 function rcmail_fix_emoticon_paths(&$mime_message)
a0109c 96 {
47124c 97   global $CONFIG;
a0109c 98
91790e 99   $body = $mime_message->getHTMLBody();
a0109c 100
S 101   // remove any null-byte characters before parsing
24ed41 102   $body = preg_replace('/\x00/', '', $body);
a0109c 103   
33ca14 104   $searchstr = 'program/js/tiny_mce/plugins/emotions/img/';
24ed41 105   $offset = 0;
a0109c 106
ed6592 107   // keep track of added images, so they're only added once
S 108   $included_images = array();
109
24ed41 110   if (preg_match_all('# src=[\'"]([^\'"]+)#', $body, $matches, PREG_OFFSET_CAPTURE)) {
A 111     foreach ($matches[1] as $m) {
112       // find emoticon image tags
113       if (preg_match('#'.$searchstr.'(.*)$#', $m[0], $imatches)) {
114         $image_name = $imatches[1];
99f2b3 115
24ed41 116         // sanitize image name so resulting attachment doesn't leave images dir
A 117         $image_name = preg_replace('/[^a-zA-Z0-9_\.\-]/i', '', $image_name);
118         $img_file = INSTALL_PATH . '/' . $searchstr . $image_name;
99f2b3 119
24ed41 120         if (! in_array($image_name, $included_images)) {
A 121           // add the image to the MIME message
91790e 122           if (! $mime_message->addHTMLImage($img_file, 'image/gif', '', true, $image_name))
24ed41 123             $OUTPUT->show_message("emoticonerror", 'error');
A 124           array_push($included_images, $image_name);
125         }
4e6eb1 126
24ed41 127         $body = substr_replace($body, $img_file, $m[1] + $offset, strlen($m[0]));
A 128         $offset += strlen($img_file) - strlen($m[0]);
ed6592 129       }
a0109c 130     }
24ed41 131   }
99f2b3 132
a0109c 133   $mime_message->setHTMLBody($body);
24ed41 134
A 135   return $body;
a0109c 136 }
41fa0b 137
751b22 138 // parse email address input (and count addresses)
e99991 139 function rcmail_email_input_format($mailto, $count=false, $check=true)
c58c0a 140 {
751b22 141   global $EMAIL_FORMAT_ERROR, $RECIPIENT_COUNT;
e4acbb 142
c58c0a 143   $regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/', '/(\S{1})(<\S+@\S+>)/U');
A 144   $replace = array(', ', ', ', '', ',', '\\1 \\2');
145
050410 146   // replace new lines and strip ending ', ', make address input more valid
c58c0a 147   $mailto = trim(preg_replace($regexp, $replace, $mailto));
A 148
050410 149   $result = array();
A 150   $items = rcube_explode_quoted_string(',', $mailto);
c58c0a 151
050410 152   foreach($items as $item) {
A 153     $item = trim($item);
154     // address in brackets without name (do nothing)
155     if (preg_match('/^<\S+@\S+>$/', $item)) {
e99991 156       $item = idn_to_ascii($item);
050410 157       $result[] = $item;
A 158     // address without brackets and without name (add brackets)
159     } else if (preg_match('/^\S+@\S+$/', $item)) {
e99991 160       $item = idn_to_ascii($item);
050410 161       $result[] = '<'.$item.'>';
A 162     // address with name (handle name)
163     } else if (preg_match('/\S+@\S+>*$/', $item, $matches)) {
164       $address = $matches[0];
165       $name = str_replace($address, '', $item);
166       $name = trim($name);
167       if ($name && ($name[0] != '"' || $name[strlen($name)-1] != '"')
c58c0a 168           && preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) {
A 169       $name = '"'.addcslashes($name, '"').'"';
170       }
e99991 171       $address = idn_to_ascii($address);
050410 172       if (!preg_match('/^<\S+@\S+>$/', $address))
A 173         $address = '<'.$address.'>';
c58c0a 174
050410 175       $result[] = $name.' '.$address;
e4acbb 176       $item = $address;
050410 177     } else if (trim($item)) {
e4acbb 178       continue;
A 179     }
180
181     // check address format
182     $item = trim($item, '<>');
e99991 183     if ($item && $check && !check_email($item)) {
e4acbb 184       $EMAIL_FORMAT_ERROR = $item;
A 185       return;
050410 186     }
A 187   }
188
751b22 189   if ($count) {
A 190     $RECIPIENT_COUNT += count($result);
191   }
192
050410 193   return implode(', ', $result);
c58c0a 194 }
acb08f 195
A 196 /****** compose message ********/
197
b068a0 198 if (strlen($_POST['_draft_saveid']) > 3)
T 199   $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST);
200
1d8cbc 201 $message_id = rcmail_gen_message_id();
4e17e6 202
5bc8cb 203 // set default charset
13c1af 204 $input_charset = $OUTPUT->get_charset();
c03095 205 $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
T 206
e4acbb 207 $EMAIL_FORMAT_ERROR = NULL;
751b22 208 $RECIPIENT_COUNT = 0;
e4acbb 209
751b22 210 $mailto = rcmail_email_input_format(get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset), true);
A 211 $mailcc = rcmail_email_input_format(get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset), true);
212 $mailbcc = rcmail_email_input_format(get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset), true);
4e17e6 213
e4acbb 214 if ($EMAIL_FORMAT_ERROR) {
d2b884 215   $OUTPUT->show_message('emailformaterror', 'error', array('email' => $EMAIL_FORMAT_ERROR));
e4acbb 216   $OUTPUT->send('iframe');
A 217 }
218
e8f8fe 219 if (empty($mailto) && !empty($mailcc)) {
T 220   $mailto = $mailcc;
221   $mailcc = null;
222 }
223 else if (empty($mailto))
224   $mailto = 'undisclosed-recipients:;';
225
d2b884 226 // Get sender name and address...
42b25a 227 $from = get_input_value('_from', RCUBE_INPUT_POST, true, $message_charset);
d2b884 228 // ... from identity...
A 229 if (is_numeric($from)) {
230   if (is_array($identity_arr = rcmail_get_identity($from))) {
231     if ($identity_arr['mailto'])
232       $from = $identity_arr['mailto'];
233     if ($identity_arr['string'])
234       $from_string = $identity_arr['string'];
235   }
236   else {
237     $from = null;
238   }
239 }
240 // ... if there is no identity record, this might be a custom from
241 else if ($from_string = rcmail_email_input_format($from)) {
242   if (preg_match('/(\S+@\S+)/', $from_string, $m))
243     $from = trim($m[1], '<>');
244   else
245     $from = null;
246 }
fd51e0 247
d2b884 248 if (!$from_string && $from)
A 249   $from_string = $from;
4e17e6 250
T 251 // compose headers array
2471d3 252 $headers = array();
A 253
254 // if configured, the Received headers goes to top, for good measure
255 if ($CONFIG['http_received_header'])
256 {
ac8edb 257   $nldlm = "\r\n\t";
ddc891 258   // FROM/VIA
2471d3 259   $http_header = 'from ';
A 260   if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
ddc891 261     $host = $_SERVER['HTTP_X_FORWARDED_FOR'];
A 262     $hostname = gethostbyaddr($host);
263     if ($CONFIG['http_received_header_encrypt']) {
264       $http_header .= rcmail_encrypt_header($hostname);
265       if ($host != $hostname)
266         $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
267     } else {
268       $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
82c98e 269       if ($host != $hostname)
A 270         $http_header .= ' (['. $host .'])';
ddc891 271     }
2471d3 272     $http_header .= $nldlm . ' via ';
A 273   }
ddc891 274   $host = $_SERVER['REMOTE_ADDR'];
A 275   $hostname = gethostbyaddr($host);
276   if ($CONFIG['http_received_header_encrypt']) {
277     $http_header .= rcmail_encrypt_header($hostname);
278     if ($host != $hostname)
279       $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
280   } else {
281     $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
82c98e 282     if ($host != $hostname)
A 283       $http_header .= ' (['. $host .'])';
ddc891 284   }
A 285   // BY
286   $http_header .= $nldlm . 'by ' . $_SERVER['HTTP_HOST'];
287   // WITH
82c98e 288   $http_header .= $nldlm . 'with HTTP (' . $_SERVER['SERVER_PROTOCOL'] .
A 289       ' '.$_SERVER['REQUEST_METHOD'] . '); ' . date('r');
2471d3 290   $http_header = wordwrap($http_header, 69, $nldlm);
ddc891 291
2471d3 292   $headers['Received'] = $http_header;
A 293 }
294
2bf3cc 295 $headers['Date'] = rcmail_user_date();
d2b884 296 $headers['From'] = rcube_charset_convert($from_string, RCMAIL_CHARSET, $message_charset);
2471d3 297 $headers['To'] = $mailto;
4e17e6 298
T 299 // additional recipients
e8f8fe 300 if (!empty($mailcc))
T 301   $headers['Cc'] = $mailcc;
4e17e6 302
e8f8fe 303 if (!empty($mailbcc))
T 304   $headers['Bcc'] = $mailbcc;
e99991 305
751b22 306 if (!empty($identity_arr['bcc'])) {
4e17e6 307   $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
751b22 308   $RECIPIENT_COUNT ++;
A 309 }
310
311 if (($max_recipients = (int) $RCMAIL->config->get('max_recipients')) > 0) {
312   if ($RECIPIENT_COUNT > $max_recipients) {
313     $OUTPUT->show_message('toomanyrecipients', 'error', array('max' => $max_recipients));
314     $OUTPUT->send('iframe');
315   }
316 }
4e17e6 317
T 318 // add subject
762a69 319 $headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, TRUE, $message_charset));
4e17e6 320
ea7c46 321 if (!empty($identity_arr['organization']))
4e17e6 322   $headers['Organization'] = $identity_arr['organization'];
T 323
7984ec 324 if (!empty($_POST['_replyto']))
8f9ab3 325   $headers['Reply-To'] = rcmail_email_input_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
7984ec 326 else if (!empty($identity_arr['reply-to']))
e99991 327   $headers['Reply-To'] = rcmail_email_input_format($identity_arr['reply-to'], false, true);
4e17e6 328
e25a35 329 if (!empty($_POST['_mailfollowupto']))
A 330   $headers['Mail-Followup-To'] = rcmail_email_input_format(get_input_value('_mailfollowupto', RCUBE_INPUT_POST, TRUE, $message_charset));
331 if (!empty($_POST['_mailreplyto']))
332   $headers['Mail-Reply-To'] = rcmail_email_input_format(get_input_value('_mailreplyto', RCUBE_INPUT_POST, TRUE, $message_charset));
333
f88d41 334 if (!empty($_SESSION['compose']['reply_msgid']))
4e17e6 335   $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
e99991 336
bbc856 337 // remember reply/forward UIDs in special headers
T 338 if (!empty($_SESSION['compose']['reply_uid']) && $savedraft)
bc404f 339   $headers['X-Draft-Info'] = array('type' => 'reply', 'uid' => $_SESSION['compose']['reply_uid']);
bbc856 340 else if (!empty($_SESSION['compose']['forward_uid']) && $savedraft)
bc404f 341   $headers['X-Draft-Info'] = array('type' => 'forward', 'uid' => $_SESSION['compose']['forward_uid']);
bbc856 342
f88d41 343 if (!empty($_SESSION['compose']['references']))
T 344   $headers['References'] = $_SESSION['compose']['references'];
4e17e6 345
d2b884 346 if (!empty($_POST['_priority'])) {
c57996 347   $priority = intval($_POST['_priority']);
3287e8 348   $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest');
4e17e6 349   if ($str_priority = $a_priorities[$priority])
T 350     $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
d2b884 351 }
4e17e6 352
d2b884 353 if (!empty($_POST['_receipt'])) {
A 354   $headers['Return-Receipt-To'] = $from_string;
355   $headers['Disposition-Notification-To'] = $from_string;
356 }
4e17e6 357
T 358 // additional headers
53e79d 359 $headers['Message-ID'] = $message_id;
A 360 $headers['X-Sender'] = $from;
361
bc404f 362 if (is_array($headers['X-Draft-Info']))
T 363   $headers['X-Draft-Info'] = rcmail_draftinfo_encode($headers['X-Draft-Info'] + array('folder' => $_SESSION['compose']['mailbox']));
364
ea7c46 365 if (!empty($CONFIG['useragent']))
4e17e6 366   $headers['User-Agent'] = $CONFIG['useragent'];
T 367
b44b4d 368 // exec hook for header checking and manipulation
e6ce00 369 $data = $RCMAIL->plugins->exec_hook('message_outgoing_headers', array('headers' => $headers));
b44b4d 370
T 371 // sending aborted by plugin
372 if ($data['abort'] && !$savedraft) {
373   $OUTPUT->show_message($data['message'] ? $data['message'] : 'sendingfailed');
374   $OUTPUT->send('iframe');
375 }
376 else
377   $headers = $data['headers'];
378
379
751b22 380 $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
940fc1 381
c03095 382 // fetch message body
ea7c46 383 $message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset);
940fc1 384
65605c 385 if (!$savedraft) {
7a48e5 386   if ($isHtml) {
A 387     // remove signature's div ID
65605c 388     $message_body = preg_replace('/\s*id="_rc_sig"/', '', $message_body);
4e17e6 389
7a48e5 390     // add inline css for blockquotes
A 391     $bstyle = 'padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%';
392     $message_body = preg_replace('/<blockquote>/',
393     '<blockquote type="cite" style="'.$bstyle.'">', $message_body);
394   }
5852c1 395
65605c 396   // generic footer for all messages
5852c1 397   if ($isHtml && !empty($CONFIG['generic_message_footer_html'])) {
A 398       $footer = file_get_contents(realpath($CONFIG['generic_message_footer_html']));
399       $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
400   }
401   else if (!empty($CONFIG['generic_message_footer'])) {
65605c 402     $footer = file_get_contents(realpath($CONFIG['generic_message_footer']));
ecb9fb 403     $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
5852c1 404     if ($isHtml)
A 405       $footer = '<pre>'.$footer.'</pre>';
65605c 406   }
5852c1 407   if ($footer)
A 408     $message_body .= "\r\n" . $footer;
65605c 409 }
a0109c 410
b62049 411 // set line length for body wrapping
c769c6 412 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
b62049 413
91790e 414 // Since we can handle big messages with disk usage, we need more time to work
A 415 @set_time_limit(0);
416
417 // create PEAR::Mail_mime instance
ac8edb 418 $MAIL_MIME = new Mail_mime("\r\n");
91790e 419
A 420 // Check if we have enough memory to handle the message in it
421 // It's faster than using files, so we'll do this if we only can
422 if (is_array($_SESSION['compose']['attachments']) && $CONFIG['smtp_server']
423   && ($mem_limit = parse_bytes(ini_get('memory_limit'))))
424 {
425   $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
426
427   foreach ($_SESSION['compose']['attachments'] as $id => $attachment)
428     $memory += $attachment['size'];
429
430   // Yeah, Net_SMTP needs up to 12x more memory, 1.33 is for base64
431   if ($memory * 1.33 * 12 > $mem_limit)
432     $MAIL_MIME->setParam('delay_file_io', true);
433 }
a0109c 434
S 435 // For HTML-formatted messages, construct the MIME message with both
436 // the HTML part and the plain-text part
437
cc97ea 438 if ($isHtml) {
e6ce00 439   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
ac8edb 440     array('body' => $message_body, 'type' => 'html', 'message' => $MAIL_MIME));
A 441
5852c1 442   $MAIL_MIME->setHTMLBody($plugin['body']);
a0109c 443
S 444   // add a plain text version of the e-mail as an alternative part.
cc97ea 445   $h2t = new html2text($plugin['body'], false, true, 0);
5852c1 446   $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n");
65605c 447   $plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
5852c1 448   if (!$plainTextPart) {
212352 449     // empty message body breaks attachment handling in drafts 
T 450     $plainTextPart = "\r\n"; 
cc97ea 451   }
ac8edb 452   else {
A 453     // make sure all line endings are CRLF (#1486712)
454     $plainTextPart = preg_replace('/\r?\n/', "\r\n", $plainTextPart);
455   }
456
e6ce00 457   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
ac8edb 458     array('body' => $plainTextPart, 'type' => 'alternative', 'message' => $MAIL_MIME));
A 459
cc97ea 460   $MAIL_MIME->setTXTBody($plugin['body']);
a0109c 461
141eb8 462   // look for "emoticon" images from TinyMCE and change their src paths to
S 463   // be file paths on the server instead of URL paths.
464   $message_body = rcmail_fix_emoticon_paths($MAIL_MIME);
cc97ea 465 }
6b6f2e 466 else {
e6ce00 467   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
5852c1 468     array('body' => $message_body, 'type' => 'plain', 'message' => $MAIL_MIME));
A 469
470   $message_body = $plugin['body'];
471
99b8c1 472   // compose format=flowed content if enabled
A 473   if ($flowed = $RCMAIL->config->get('send_format_flowed', true))
474     $message_body = rcube_message::format_flowed($message_body, min($LINE_LENGTH+2, 79));
6b6f2e 475   else
dffcaa 476     $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n");
5852c1 477
a23884 478   $message_body = wordwrap($message_body, 998, "\r\n", true);
cc97ea 479   if (!strlen($message_body)) { 
212352 480     // empty message body breaks attachment handling in drafts 
T 481     $message_body = "\r\n"; 
a0109c 482   }
ac8edb 483
5852c1 484   $MAIL_MIME->setTXTBody($message_body, false, true);
cc97ea 485 }
4e17e6 486
T 487 // add stored attachments, if any
91790e 488 if (is_array($_SESSION['compose']['attachments']))
A 489 {
cc97ea 490   foreach ($_SESSION['compose']['attachments'] as $id => $attachment) {
T 491     // This hook retrieves the attachment contents from the file storage backend
e6ce00 492     $attachment = $RCMAIL->plugins->exec_hook('attachment_get', $attachment);
cc97ea 493
c973ab 494     $dispurl = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]*/';
cc97ea 495     $message_body = $MAIL_MIME->getHTMLBody();
T 496     if ($isHtml && (preg_match($dispurl, $message_body) > 0)) {
d519ef 497       $message_body = preg_replace($dispurl, ' src="'.$attachment['name'].'" ', $message_body);
7145e0 498       $MAIL_MIME->setHTMLBody($message_body);
dc9d75 499
cc97ea 500       if ($attachment['data'])
T 501         $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false);
502       else
503         $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true);
4315b0 504     }
cc97ea 505     else {
6d5dba 506       $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914
cc97ea 507       $file = $attachment['data'] ? $attachment['data'] : $attachment['path'];
33ca14 508
A 509       // .eml attachments send inline
cc97ea 510       $MAIL_MIME->addAttachment($file,
57388f 511         $ctype,
cc97ea 512         $attachment['name'],
T 513         ($attachment['data'] ? false : true),
dc9d75 514         ($ctype == 'message/rfc822' ? '8bit' : 'base64'),
33ca14 515         ($ctype == 'message/rfc822' ? 'inline' : 'attachment'),
53604a 516         '', '', '',
cc97ea 517         $CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
53604a 518         $CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL,
A 519         '', RCMAIL_CHARSET
cc97ea 520       );
4315b0 521     }
S 522   }
cc97ea 523 }
4e17e6 524
3d0ec7 525 // choose transfer encoding for plain/text body
A 526 if (preg_match('/[^\x00-\x7F]/', $MAIL_MIME->getTXTBody()))
47ad83 527   $transfer_encoding = $RCMAIL->config->get('force_7bit') ? 'quoted-printable' : '8bit';
3d0ec7 528 else
A 529   $transfer_encoding = '7bit';
530
a95e0e 531 // encoding settings for mail composing
91790e 532 $MAIL_MIME->setParam('text_encoding', $transfer_encoding);
A 533 $MAIL_MIME->setParam('html_encoding', 'quoted-printable');
534 $MAIL_MIME->setParam('head_encoding', 'quoted-printable');
535 $MAIL_MIME->setParam('head_charset', $message_charset);
536 $MAIL_MIME->setParam('html_charset', $message_charset);
6b6f2e 537 $MAIL_MIME->setParam('text_charset', $message_charset . ($flowed ? ";\r\n format=flowed" : ''));
cc97ea 538
5a6ad2 539 // encoding subject header with mb_encode provides better results with asian characters
d2b884 540 if (function_exists('mb_encode_mimeheader')) {
24fe97 541   mb_internal_encoding($message_charset);
34b659 542   $headers['Subject'] = mb_encode_mimeheader($headers['Subject'],
ac8edb 543     $message_charset, 'Q', "\r\n", 8);
f11541 544   mb_internal_encoding(RCMAIL_CHARSET);
b517af 545 }
4e17e6 546
fba1f5 547 // pass headers to message object
T 548 $MAIL_MIME->headers($headers);
4e17e6 549
d2b884 550 // Begin SMTP Delivery Block
fba1f5 551 if (!$savedraft)
T 552 {
d2b884 553   // check 'From' address (identity may be incomplete)
A 554   if (empty($from)) {
fd51e0 555     $OUTPUT->show_message('nofromaddress', 'error');
d2b884 556     $OUTPUT->send('iframe');
fd51e0 557   }
A 558
f22ea7 559   // Handle Delivery Status Notification request
A 560   if (!empty($_POST['_dsn'])) {
561     $smtp_opts['dsn'] = true;
562   }
563
564   $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto,
565     $smtp_error, $mailbody_file, $smtp_opts);
91790e 566
1966c5 567   // return to compose page if sending failed
968bdc 568   if (!$sent)
4e17e6 569     {
91790e 570     // remove temp file
A 571     if ($mailbody_file) {
572       unlink($mailbody_file);
573       }
574
2818f8 575     if ($smtp_error)
A 576       $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']); 
577     else
578       $OUTPUT->show_message('sendingfailed', 'error'); 
f11541 579     $OUTPUT->send('iframe');
4e17e6 580     }
acb08f 581
A 582   // save message sent time
583   if (!empty($CONFIG['sendmail_delay']))
4b60fa 584     $RCMAIL->user->save_prefs(array('last_message_time' => time()));
1966c5 585   
4dae73 586   // set replied/forwarded flag
1966c5 587   if ($_SESSION['compose']['reply_uid'])
48958e 588     $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED', $_SESSION['compose']['mailbox']);
4dae73 589   else if ($_SESSION['compose']['forward_uid'])
48958e 590     $IMAP->set_flag($_SESSION['compose']['forward_uid'], 'FORWARDED', $_SESSION['compose']['mailbox']);
c03095 591
4dae73 592 } // End of SMTP Delivery Block
41fa0b 593
T 594
1966c5 595 // Determine which folder to save message
b068a0 596 if ($savedraft)
faf876 597   $store_target = $CONFIG['drafts_mbox'];
d583bc 598 else    
faf876 599   $store_target = isset($_POST['_store_target']) ? get_input_value('_store_target', RCUBE_INPUT_POST) : $CONFIG['sent_mbox'];
c03095 600
faf876 601 if ($store_target)
4e17e6 602   {
16378f 603   // check if folder is subscribed
A 604   if ($IMAP->mailbox_exists($store_target, true))
605     $store_folder = true;
606   // folder may be existing but not subscribed (#1485241)
607   else if (!$IMAP->mailbox_exists($store_target))
608     $store_folder = $IMAP->create_mailbox($store_target, true);
609   else if ($IMAP->subscribe($store_target))
610     $store_folder = true;
520c36 611
91790e 612   // append message to sent box
A 613   if ($store_folder) {
614
615     // message body in file
616     if ($mailbody_file || $MAIL_MIME->getParam('delay_file_io')) {
617       $headers = $MAIL_MIME->txtHeaders();
618       
619       // file already created
620       if ($mailbody_file)
621         $msg = $mailbody_file;
622       else {
623         $temp_dir = $RCMAIL->config->get('temp_dir');
624         $mailbody_file = tempnam($temp_dir, 'rcmMsg');
625         if (!PEAR::isError($msg = $MAIL_MIME->saveMessageBody($mailbody_file)))
626           $msg = $mailbody_file;
627         }
628       }
629     else {
630       $msg = $MAIL_MIME->getMessage();
631       $headers = '';
632       }
633
634     if (PEAR::isError($msg))
635       raise_error(array('code' => 600, 'type' => 'php',
636         'file' => __FILE__, 'line' => __LINE__,
637             'message' => "Could not create message: ".$msg->getMessage()),
638             TRUE, FALSE);
639     else {
640       $saved = $IMAP->save_message($store_target, $msg, $headers, $mailbody_file ? true : false);
641       }
642
643     if ($mailbody_file) {
644       unlink($mailbody_file);
645       $mailbody_file = null;
646       }
647
648     // raise error if saving failed
649     if (!$saved) {
650       raise_error(array('code' => 800, 'type' => 'imap',
6d13ca 651         'file' => __FILE__, 'line' => __LINE__,
A 652             'message' => "Could not save message in $store_target"), TRUE, FALSE);
41fa0b 653     
91790e 654       if ($savedraft) {
A 655         $OUTPUT->show_message('errorsaving', 'error');
656         $OUTPUT->send('iframe');
657         }
9a5762 658       }
f0f98f 659     }
4e17e6 660
b068a0 661   if ($olddraftmessageid)
T 662     {
1966c5 663     // delete previous saved draft
6f31b3 664     $a_deleteid = $IMAP->search_once($CONFIG['drafts_mbox'],
A 665         'HEADER Message-ID '.$olddraftmessageid, true);
666     $deleted = $IMAP->delete_message($a_deleteid, $CONFIG['drafts_mbox']);
4e17e6 667
f0f98f 668     // raise error if deletion of old draft failed
1966c5 669     if (!$deleted)
6d13ca 670       raise_error(array('code' => 800, 'type' => 'imap',
A 671             'file' => __FILE__, 'line' => __LINE__,
672                 'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE);
4e17e6 673     }
T 674   }
91790e 675 // remove temp file
A 676 else if ($mailbody_file) {
677   unlink($mailbody_file);
678   }
679
4e17e6 680
1966c5 681 if ($savedraft)
S 682   {
c719f3 683   $msgid = strtr($message_id, array('>' => '', '<' => ''));
T 684   
685   // remember new draft-uid
6f31b3 686   $draftuids = $IMAP->search_once($CONFIG['drafts_mbox'], 'HEADER Message-ID '.$msgid, true);
A 687   $_SESSION['compose']['param']['_draft_uid'] = $draftuids[0];
c719f3 688
f11541 689   // display success
T 690   $OUTPUT->show_message('messagesaved', 'confirmation');
f0f98f 691
f11541 692   // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
c719f3 693   $OUTPUT->command('set_draft_id', $msgid);
f11541 694   $OUTPUT->command('compose_field_hash', true);
41fa0b 695
f0f98f 696   // start the auto-save timer again
f11541 697   $OUTPUT->command('auto_save_start');
f0f98f 698
f11541 699   $OUTPUT->send('iframe');
1966c5 700   }
S 701 else
702   {
703   rcmail_compose_cleanup();
9a5762 704
A 705   if ($store_folder && !$saved)
706     $OUTPUT->command('sent_successfully', 'error', rcube_label('errorsavingsent'));
707   else
708     $OUTPUT->command('sent_successfully', 'confirmation', rcube_label('messagesent'));
f11541 709   $OUTPUT->send('iframe');
1966c5 710   }
4e17e6 711
b25dfd 712