alecpl
2010-10-13 ad334a12579f21e4e04a47e257b9d0ac71d50b98
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) {
A 215   $OUTPUT->show_message('emailformaterror', 'error', array('email' => $EMAIL_FORMAT_ERROR)); 
216   $OUTPUT->send('iframe');
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
226 // get sender name and address
42b25a 227 $from = get_input_value('_from', RCUBE_INPUT_POST, true, $message_charset);
fd51e0 228 $identity_arr = rcmail_get_identity($from);
A 229
8f9ab3 230 if (!$identity_arr && ($from = rcmail_email_input_format($from))) {
42b25a 231   if (preg_match('/(\S+@\S+)/', $from, $m))
8f9ab3 232     $identity_arr['mailto'] = $m[1];
A 233 } else
fd51e0 234   $from = $identity_arr['mailto'];
4e17e6 235
b068a0 236 if (empty($identity_arr['string']))
T 237   $identity_arr['string'] = $from;
4e17e6 238
T 239 // compose headers array
2471d3 240 $headers = array();
A 241
242 // if configured, the Received headers goes to top, for good measure
243 if ($CONFIG['http_received_header'])
244 {
ac8edb 245   $nldlm = "\r\n\t";
ddc891 246   // FROM/VIA
2471d3 247   $http_header = 'from ';
A 248   if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
ddc891 249     $host = $_SERVER['HTTP_X_FORWARDED_FOR'];
A 250     $hostname = gethostbyaddr($host);
251     if ($CONFIG['http_received_header_encrypt']) {
252       $http_header .= rcmail_encrypt_header($hostname);
253       if ($host != $hostname)
254         $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
255     } else {
256       $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
82c98e 257       if ($host != $hostname)
A 258         $http_header .= ' (['. $host .'])';
ddc891 259     }
2471d3 260     $http_header .= $nldlm . ' via ';
A 261   }
ddc891 262   $host = $_SERVER['REMOTE_ADDR'];
A 263   $hostname = gethostbyaddr($host);
264   if ($CONFIG['http_received_header_encrypt']) {
265     $http_header .= rcmail_encrypt_header($hostname);
266     if ($host != $hostname)
267       $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
268   } else {
269     $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
82c98e 270     if ($host != $hostname)
A 271       $http_header .= ' (['. $host .'])';
ddc891 272   }
A 273   // BY
274   $http_header .= $nldlm . 'by ' . $_SERVER['HTTP_HOST'];
275   // WITH
82c98e 276   $http_header .= $nldlm . 'with HTTP (' . $_SERVER['SERVER_PROTOCOL'] .
A 277       ' '.$_SERVER['REQUEST_METHOD'] . '); ' . date('r');
2471d3 278   $http_header = wordwrap($http_header, 69, $nldlm);
ddc891 279
2471d3 280   $headers['Received'] = $http_header;
A 281 }
282
2bf3cc 283 $headers['Date'] = rcmail_user_date();
2471d3 284 $headers['From'] = rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset);
A 285 $headers['To'] = $mailto;
4e17e6 286
T 287 // additional recipients
e8f8fe 288 if (!empty($mailcc))
T 289   $headers['Cc'] = $mailcc;
4e17e6 290
e8f8fe 291 if (!empty($mailbcc))
T 292   $headers['Bcc'] = $mailbcc;
e99991 293
751b22 294 if (!empty($identity_arr['bcc'])) {
4e17e6 295   $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
751b22 296   $RECIPIENT_COUNT ++;
A 297 }
298
299 if (($max_recipients = (int) $RCMAIL->config->get('max_recipients')) > 0) {
300   if ($RECIPIENT_COUNT > $max_recipients) {
301     $OUTPUT->show_message('toomanyrecipients', 'error', array('max' => $max_recipients));
302     $OUTPUT->send('iframe');
303   }
304 }
4e17e6 305
T 306 // add subject
762a69 307 $headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, TRUE, $message_charset));
4e17e6 308
ea7c46 309 if (!empty($identity_arr['organization']))
4e17e6 310   $headers['Organization'] = $identity_arr['organization'];
T 311
7984ec 312 if (!empty($_POST['_replyto']))
8f9ab3 313   $headers['Reply-To'] = rcmail_email_input_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
7984ec 314 else if (!empty($identity_arr['reply-to']))
e99991 315   $headers['Reply-To'] = rcmail_email_input_format($identity_arr['reply-to'], false, true);
4e17e6 316
e25a35 317 if (!empty($_POST['_mailfollowupto']))
A 318   $headers['Mail-Followup-To'] = rcmail_email_input_format(get_input_value('_mailfollowupto', RCUBE_INPUT_POST, TRUE, $message_charset));
319 if (!empty($_POST['_mailreplyto']))
320   $headers['Mail-Reply-To'] = rcmail_email_input_format(get_input_value('_mailreplyto', RCUBE_INPUT_POST, TRUE, $message_charset));
321
f88d41 322 if (!empty($_SESSION['compose']['reply_msgid']))
4e17e6 323   $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
e99991 324
bbc856 325 // remember reply/forward UIDs in special headers
T 326 if (!empty($_SESSION['compose']['reply_uid']) && $savedraft)
bc404f 327   $headers['X-Draft-Info'] = array('type' => 'reply', 'uid' => $_SESSION['compose']['reply_uid']);
bbc856 328 else if (!empty($_SESSION['compose']['forward_uid']) && $savedraft)
bc404f 329   $headers['X-Draft-Info'] = array('type' => 'forward', 'uid' => $_SESSION['compose']['forward_uid']);
bbc856 330
f88d41 331 if (!empty($_SESSION['compose']['references']))
T 332   $headers['References'] = $_SESSION['compose']['references'];
4e17e6 333
ea7c46 334 if (!empty($_POST['_priority']))
4e17e6 335   {
c57996 336   $priority = intval($_POST['_priority']);
3287e8 337   $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest');
4e17e6 338   if ($str_priority = $a_priorities[$priority])
T 339     $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
340   }
341
620439 342 if (!empty($_POST['_receipt']))
T 343   {
344   $headers['Return-Receipt-To'] = $identity_arr['string'];
345   $headers['Disposition-Notification-To'] = $identity_arr['string'];
346   }
4e17e6 347
T 348 // additional headers
53e79d 349 $headers['Message-ID'] = $message_id;
A 350 $headers['X-Sender'] = $from;
351
bc404f 352 if (is_array($headers['X-Draft-Info']))
T 353   $headers['X-Draft-Info'] = rcmail_draftinfo_encode($headers['X-Draft-Info'] + array('folder' => $_SESSION['compose']['mailbox']));
354
ea7c46 355 if (!empty($CONFIG['useragent']))
4e17e6 356   $headers['User-Agent'] = $CONFIG['useragent'];
T 357
b44b4d 358 // exec hook for header checking and manipulation
e6ce00 359 $data = $RCMAIL->plugins->exec_hook('message_outgoing_headers', array('headers' => $headers));
b44b4d 360
T 361 // sending aborted by plugin
362 if ($data['abort'] && !$savedraft) {
363   $OUTPUT->show_message($data['message'] ? $data['message'] : 'sendingfailed');
364   $OUTPUT->send('iframe');
365 }
366 else
367   $headers = $data['headers'];
368
369
751b22 370 $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
940fc1 371
c03095 372 // fetch message body
ea7c46 373 $message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset);
940fc1 374
65605c 375 if (!$savedraft) {
7a48e5 376   if ($isHtml) {
A 377     // remove signature's div ID
65605c 378     $message_body = preg_replace('/\s*id="_rc_sig"/', '', $message_body);
4e17e6 379
7a48e5 380     // add inline css for blockquotes
A 381     $bstyle = 'padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%';
382     $message_body = preg_replace('/<blockquote>/',
383     '<blockquote type="cite" style="'.$bstyle.'">', $message_body);
384   }
5852c1 385
65605c 386   // generic footer for all messages
5852c1 387   if ($isHtml && !empty($CONFIG['generic_message_footer_html'])) {
A 388       $footer = file_get_contents(realpath($CONFIG['generic_message_footer_html']));
389       $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
390   }
391   else if (!empty($CONFIG['generic_message_footer'])) {
65605c 392     $footer = file_get_contents(realpath($CONFIG['generic_message_footer']));
ecb9fb 393     $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
5852c1 394     if ($isHtml)
A 395       $footer = '<pre>'.$footer.'</pre>';
65605c 396   }
5852c1 397   if ($footer)
A 398     $message_body .= "\r\n" . $footer;
65605c 399 }
a0109c 400
b62049 401 // set line length for body wrapping
c769c6 402 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
b62049 403
91790e 404 // Since we can handle big messages with disk usage, we need more time to work
A 405 @set_time_limit(0);
406
407 // create PEAR::Mail_mime instance
ac8edb 408 $MAIL_MIME = new Mail_mime("\r\n");
91790e 409
A 410 // Check if we have enough memory to handle the message in it
411 // It's faster than using files, so we'll do this if we only can
412 if (is_array($_SESSION['compose']['attachments']) && $CONFIG['smtp_server']
413   && ($mem_limit = parse_bytes(ini_get('memory_limit'))))
414 {
415   $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
416
417   foreach ($_SESSION['compose']['attachments'] as $id => $attachment)
418     $memory += $attachment['size'];
419
420   // Yeah, Net_SMTP needs up to 12x more memory, 1.33 is for base64
421   if ($memory * 1.33 * 12 > $mem_limit)
422     $MAIL_MIME->setParam('delay_file_io', true);
423 }
a0109c 424
S 425 // For HTML-formatted messages, construct the MIME message with both
426 // the HTML part and the plain-text part
427
cc97ea 428 if ($isHtml) {
e6ce00 429   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
ac8edb 430     array('body' => $message_body, 'type' => 'html', 'message' => $MAIL_MIME));
A 431
5852c1 432   $MAIL_MIME->setHTMLBody($plugin['body']);
a0109c 433
S 434   // add a plain text version of the e-mail as an alternative part.
cc97ea 435   $h2t = new html2text($plugin['body'], false, true, 0);
5852c1 436   $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n");
65605c 437   $plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
5852c1 438   if (!$plainTextPart) {
212352 439     // empty message body breaks attachment handling in drafts 
T 440     $plainTextPart = "\r\n"; 
cc97ea 441   }
ac8edb 442   else {
A 443     // make sure all line endings are CRLF (#1486712)
444     $plainTextPart = preg_replace('/\r?\n/', "\r\n", $plainTextPart);
445   }
446
e6ce00 447   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
ac8edb 448     array('body' => $plainTextPart, 'type' => 'alternative', 'message' => $MAIL_MIME));
A 449
cc97ea 450   $MAIL_MIME->setTXTBody($plugin['body']);
a0109c 451
141eb8 452   // look for "emoticon" images from TinyMCE and change their src paths to
S 453   // be file paths on the server instead of URL paths.
454   $message_body = rcmail_fix_emoticon_paths($MAIL_MIME);
cc97ea 455 }
6b6f2e 456 else {
e6ce00 457   $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
5852c1 458     array('body' => $message_body, 'type' => 'plain', 'message' => $MAIL_MIME));
A 459
460   $message_body = $plugin['body'];
461
99b8c1 462   // compose format=flowed content if enabled
A 463   if ($flowed = $RCMAIL->config->get('send_format_flowed', true))
464     $message_body = rcube_message::format_flowed($message_body, min($LINE_LENGTH+2, 79));
6b6f2e 465   else
dffcaa 466     $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n");
5852c1 467
a23884 468   $message_body = wordwrap($message_body, 998, "\r\n", true);
cc97ea 469   if (!strlen($message_body)) { 
212352 470     // empty message body breaks attachment handling in drafts 
T 471     $message_body = "\r\n"; 
a0109c 472   }
ac8edb 473
5852c1 474   $MAIL_MIME->setTXTBody($message_body, false, true);
cc97ea 475 }
4e17e6 476
T 477 // add stored attachments, if any
91790e 478 if (is_array($_SESSION['compose']['attachments']))
A 479 {
cc97ea 480   foreach ($_SESSION['compose']['attachments'] as $id => $attachment) {
T 481     // This hook retrieves the attachment contents from the file storage backend
e6ce00 482     $attachment = $RCMAIL->plugins->exec_hook('attachment_get', $attachment);
cc97ea 483
c973ab 484     $dispurl = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]*/';
cc97ea 485     $message_body = $MAIL_MIME->getHTMLBody();
T 486     if ($isHtml && (preg_match($dispurl, $message_body) > 0)) {
d519ef 487       $message_body = preg_replace($dispurl, ' src="'.$attachment['name'].'" ', $message_body);
7145e0 488       $MAIL_MIME->setHTMLBody($message_body);
dc9d75 489
cc97ea 490       if ($attachment['data'])
T 491         $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false);
492       else
493         $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true);
4315b0 494     }
cc97ea 495     else {
6d5dba 496       $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914
cc97ea 497       $file = $attachment['data'] ? $attachment['data'] : $attachment['path'];
33ca14 498
A 499       // .eml attachments send inline
cc97ea 500       $MAIL_MIME->addAttachment($file,
8b36d4 501         $ctype, 
cc97ea 502         $attachment['name'],
T 503         ($attachment['data'] ? false : true),
dc9d75 504         ($ctype == 'message/rfc822' ? '8bit' : 'base64'),
33ca14 505         ($ctype == 'message/rfc822' ? 'inline' : 'attachment'),
ffae15 506         $message_charset, '', '', 
cc97ea 507         $CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
T 508         $CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL
509       );
4315b0 510     }
S 511   }
cc97ea 512 }
4e17e6 513
3d0ec7 514 // choose transfer encoding for plain/text body
A 515 if (preg_match('/[^\x00-\x7F]/', $MAIL_MIME->getTXTBody()))
47ad83 516   $transfer_encoding = $RCMAIL->config->get('force_7bit') ? 'quoted-printable' : '8bit';
3d0ec7 517 else
A 518   $transfer_encoding = '7bit';
519
a95e0e 520 // encoding settings for mail composing
91790e 521 $MAIL_MIME->setParam('text_encoding', $transfer_encoding);
A 522 $MAIL_MIME->setParam('html_encoding', 'quoted-printable');
523 $MAIL_MIME->setParam('head_encoding', 'quoted-printable');
524 $MAIL_MIME->setParam('head_charset', $message_charset);
525 $MAIL_MIME->setParam('html_charset', $message_charset);
6b6f2e 526 $MAIL_MIME->setParam('text_charset', $message_charset . ($flowed ? ";\r\n format=flowed" : ''));
cc97ea 527
5a6ad2 528 // encoding subject header with mb_encode provides better results with asian characters
34b659 529 if (function_exists('mb_encode_mimeheader'))
b517af 530 {
24fe97 531   mb_internal_encoding($message_charset);
34b659 532   $headers['Subject'] = mb_encode_mimeheader($headers['Subject'],
ac8edb 533     $message_charset, 'Q', "\r\n", 8);
f11541 534   mb_internal_encoding(RCMAIL_CHARSET);
b517af 535 }
4e17e6 536
fba1f5 537 // pass headers to message object
T 538 $MAIL_MIME->headers($headers);
4e17e6 539
fba1f5 540 // Begin SMTP Delivery Block 
T 541 if (!$savedraft)
542 {
fd51e0 543   // check for 'From' address (identity may be incomplete)
A 544   if ($identity_arr && !$identity_arr['mailto']) {
545     $OUTPUT->show_message('nofromaddress', 'error');
546     $OUTPUT->send('iframe'); 
547   }
548
f22ea7 549   // Handle Delivery Status Notification request
A 550   if (!empty($_POST['_dsn'])) {
551     $smtp_opts['dsn'] = true;
552   }
553
554   $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto,
555     $smtp_error, $mailbody_file, $smtp_opts);
91790e 556
1966c5 557   // return to compose page if sending failed
968bdc 558   if (!$sent)
4e17e6 559     {
91790e 560     // remove temp file
A 561     if ($mailbody_file) {
562       unlink($mailbody_file);
563       }
564
2818f8 565     if ($smtp_error)
A 566       $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']); 
567     else
568       $OUTPUT->show_message('sendingfailed', 'error'); 
f11541 569     $OUTPUT->send('iframe');
4e17e6 570     }
acb08f 571
A 572   // save message sent time
573   if (!empty($CONFIG['sendmail_delay']))
4b60fa 574     $RCMAIL->user->save_prefs(array('last_message_time' => time()));
1966c5 575   
4dae73 576   // set replied/forwarded flag
1966c5 577   if ($_SESSION['compose']['reply_uid'])
48958e 578     $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED', $_SESSION['compose']['mailbox']);
4dae73 579   else if ($_SESSION['compose']['forward_uid'])
48958e 580     $IMAP->set_flag($_SESSION['compose']['forward_uid'], 'FORWARDED', $_SESSION['compose']['mailbox']);
c03095 581
4dae73 582 } // End of SMTP Delivery Block
41fa0b 583
T 584
1966c5 585 // Determine which folder to save message
b068a0 586 if ($savedraft)
faf876 587   $store_target = $CONFIG['drafts_mbox'];
d583bc 588 else    
faf876 589   $store_target = isset($_POST['_store_target']) ? get_input_value('_store_target', RCUBE_INPUT_POST) : $CONFIG['sent_mbox'];
c03095 590
faf876 591 if ($store_target)
4e17e6 592   {
16378f 593   // check if folder is subscribed
A 594   if ($IMAP->mailbox_exists($store_target, true))
595     $store_folder = true;
596   // folder may be existing but not subscribed (#1485241)
597   else if (!$IMAP->mailbox_exists($store_target))
598     $store_folder = $IMAP->create_mailbox($store_target, true);
599   else if ($IMAP->subscribe($store_target))
600     $store_folder = true;
520c36 601
91790e 602   // append message to sent box
A 603   if ($store_folder) {
604
605     // message body in file
606     if ($mailbody_file || $MAIL_MIME->getParam('delay_file_io')) {
607       $headers = $MAIL_MIME->txtHeaders();
608       
609       // file already created
610       if ($mailbody_file)
611         $msg = $mailbody_file;
612       else {
613         $temp_dir = $RCMAIL->config->get('temp_dir');
614         $mailbody_file = tempnam($temp_dir, 'rcmMsg');
615         if (!PEAR::isError($msg = $MAIL_MIME->saveMessageBody($mailbody_file)))
616           $msg = $mailbody_file;
617         }
618       }
619     else {
620       $msg = $MAIL_MIME->getMessage();
621       $headers = '';
622       }
623
624     if (PEAR::isError($msg))
625       raise_error(array('code' => 600, 'type' => 'php',
626         'file' => __FILE__, 'line' => __LINE__,
627             'message' => "Could not create message: ".$msg->getMessage()),
628             TRUE, FALSE);
629     else {
630       $saved = $IMAP->save_message($store_target, $msg, $headers, $mailbody_file ? true : false);
631       }
632
633     if ($mailbody_file) {
634       unlink($mailbody_file);
635       $mailbody_file = null;
636       }
637
638     // raise error if saving failed
639     if (!$saved) {
640       raise_error(array('code' => 800, 'type' => 'imap',
6d13ca 641         'file' => __FILE__, 'line' => __LINE__,
A 642             'message' => "Could not save message in $store_target"), TRUE, FALSE);
41fa0b 643     
91790e 644       if ($savedraft) {
A 645         $OUTPUT->show_message('errorsaving', 'error');
646         $OUTPUT->send('iframe');
647         }
9a5762 648       }
f0f98f 649     }
4e17e6 650
b068a0 651   if ($olddraftmessageid)
T 652     {
1966c5 653     // delete previous saved draft
6f31b3 654     $a_deleteid = $IMAP->search_once($CONFIG['drafts_mbox'],
A 655         'HEADER Message-ID '.$olddraftmessageid, true);
656     $deleted = $IMAP->delete_message($a_deleteid, $CONFIG['drafts_mbox']);
4e17e6 657
f0f98f 658     // raise error if deletion of old draft failed
1966c5 659     if (!$deleted)
6d13ca 660       raise_error(array('code' => 800, 'type' => 'imap',
A 661             'file' => __FILE__, 'line' => __LINE__,
662                 'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE);
4e17e6 663     }
T 664   }
91790e 665 // remove temp file
A 666 else if ($mailbody_file) {
667   unlink($mailbody_file);
668   }
669
4e17e6 670
1966c5 671 if ($savedraft)
S 672   {
c719f3 673   $msgid = strtr($message_id, array('>' => '', '<' => ''));
T 674   
675   // remember new draft-uid
6f31b3 676   $draftuids = $IMAP->search_once($CONFIG['drafts_mbox'], 'HEADER Message-ID '.$msgid, true);
A 677   $_SESSION['compose']['param']['_draft_uid'] = $draftuids[0];
c719f3 678
f11541 679   // display success
T 680   $OUTPUT->show_message('messagesaved', 'confirmation');
f0f98f 681
f11541 682   // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
c719f3 683   $OUTPUT->command('set_draft_id', $msgid);
f11541 684   $OUTPUT->command('compose_field_hash', true);
41fa0b 685
f0f98f 686   // start the auto-save timer again
f11541 687   $OUTPUT->command('auto_save_start');
f0f98f 688
f11541 689   $OUTPUT->send('iframe');
1966c5 690   }
S 691 else
692   {
693   rcmail_compose_cleanup();
9a5762 694
A 695   if ($store_folder && !$saved)
696     $OUTPUT->command('sent_successfully', 'error', rcube_label('errorsavingsent'));
697   else
698     $OUTPUT->command('sent_successfully', 'confirmation', rcube_label('messagesent'));
f11541 699   $OUTPUT->send('iframe');
1966c5 700   }
4e17e6 701
b25dfd 702