4 +-----------------------------------------------------------------------+
5 | program/steps/mail/sendmail.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2010, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Compose a new mail message with all headers and attachments |
13 | and send it using the PEAR::Net_SMTP class or with PHP mail() |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
19 $Id: sendmail.inc 5229 2011-09-16 19:13:27Z thomasb $
23 // remove all scripts and act as called in frame
25 $OUTPUT->framed = TRUE;
27 $savedraft = !empty($_POST['_draft']) ? true : false;
29 $COMPOSE_ID = get_input_value('_id', RCUBE_INPUT_GPC);
30 $_SESSION['compose'] = $_SESSION['compose_data_'.$COMPOSE_ID];
32 /****** checks ********/
34 if (!isset($_SESSION['compose']['id'])) {
35 raise_error(array('code' => 500, 'type' => 'php',
36 'file' => __FILE__, 'line' => __LINE__,
37 'message' => "Invalid compose ID"), true, false);
39 $OUTPUT->show_message('internalerror', 'error');
40 $OUTPUT->send('iframe');
44 if (empty($_POST['_to']) && empty($_POST['_cc']) && empty($_POST['_bcc'])
45 && empty($_POST['_subject']) && $_POST['_message']) {
46 $OUTPUT->show_message('sendingfailed', 'error');
47 $OUTPUT->send('iframe');
50 if(!empty($CONFIG['sendmail_delay'])) {
51 $wait_sec = time() - intval($CONFIG['sendmail_delay']) - intval($CONFIG['last_message_time']);
53 $OUTPUT->show_message('senttooquickly', 'error', array('sec' => $wait_sec * -1));
54 $OUTPUT->send('iframe');
60 /****** message sending functions ********/
62 // encrypt parts of the header
63 function rcmail_encrypt_header($what)
65 global $CONFIG, $RCMAIL;
66 if (!$CONFIG['http_received_header_encrypt']) {
69 return $RCMAIL->encrypt($what);
72 // get identity record
73 function rcmail_get_identity($id)
75 global $USER, $OUTPUT;
77 if ($sql_arr = $USER->get_identity($id)) {
79 $out['mailto'] = $sql_arr['email'];
80 $out['string'] = format_email_recipient($sql_arr['email'],
81 rcube_charset_convert($sql_arr['name'], RCMAIL_CHARSET, $OUTPUT->get_charset()));
91 * <img src="http[s]://.../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
95 * <img src="/path/on/server/.../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
98 function rcmail_fix_emoticon_paths(&$mime_message)
102 $body = $mime_message->getHTMLBody();
104 // remove any null-byte characters before parsing
105 $body = preg_replace('/\x00/', '', $body);
107 $searchstr = 'program/js/tiny_mce/plugins/emotions/img/';
110 // keep track of added images, so they're only added once
111 $included_images = array();
113 if (preg_match_all('# src=[\'"]([^\'"]+)#', $body, $matches, PREG_OFFSET_CAPTURE)) {
114 foreach ($matches[1] as $m) {
115 // find emoticon image tags
116 if (preg_match('#'.$searchstr.'(.*)$#', $m[0], $imatches)) {
117 $image_name = $imatches[1];
119 // sanitize image name so resulting attachment doesn't leave images dir
120 $image_name = preg_replace('/[^a-zA-Z0-9_\.\-]/i', '', $image_name);
121 $img_file = INSTALL_PATH . '/' . $searchstr . $image_name;
123 if (! in_array($image_name, $included_images)) {
124 // add the image to the MIME message
125 if (! $mime_message->addHTMLImage($img_file, 'image/gif', '', true, $image_name))
126 $OUTPUT->show_message("emoticonerror", 'error');
127 array_push($included_images, $image_name);
130 $body = substr_replace($body, $img_file, $m[1] + $offset, strlen($m[0]));
131 $offset += strlen($img_file) - strlen($m[0]);
136 $mime_message->setHTMLBody($body);
141 // parse email address input (and count addresses)
142 function rcmail_email_input_format($mailto, $count=false, $check=true)
144 global $EMAIL_FORMAT_ERROR, $RECIPIENT_COUNT;
146 // simplified email regexp, supporting quoted local part
147 $email_regexp = '(\S+|("[^"]+"))@\S+';
149 $regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/', '/(\S{1})(<'.$email_regexp.'>)/U');
150 $replace = array(', ', ', ', '', ',', '\\1 \\2');
152 // replace new lines and strip ending ', ', make address input more valid
153 $mailto = trim(preg_replace($regexp, $replace, $mailto));
156 $items = rcube_explode_quoted_string(',', $mailto);
158 foreach($items as $item) {
160 // address in brackets without name (do nothing)
161 if (preg_match('/^<'.$email_regexp.'>$/', $item)) {
162 $item = rcube_idn_to_ascii($item);
164 // address without brackets and without name (add brackets)
165 } else if (preg_match('/^'.$email_regexp.'$/', $item)) {
166 $item = rcube_idn_to_ascii($item);
167 $result[] = '<'.$item.'>';
168 // address with name (handle name)
169 } else if (preg_match('/'.$email_regexp.'>*$/', $item, $matches)) {
170 $address = $matches[0];
171 $name = str_replace($address, '', $item);
173 if ($name && ($name[0] != '"' || $name[strlen($name)-1] != '"')
174 && preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) {
175 $name = '"'.addcslashes($name, '"').'"';
177 $address = rcube_idn_to_ascii($address);
178 if (!preg_match('/^<'.$email_regexp.'>$/', $address))
179 $address = '<'.$address.'>';
181 $result[] = $name.' '.$address;
183 } else if (trim($item)) {
187 // check address format
188 $item = trim($item, '<>');
189 if ($item && $check && !check_email($item)) {
190 $EMAIL_FORMAT_ERROR = $item;
196 $RECIPIENT_COUNT += count($result);
199 return implode(', ', $result);
203 /****** compose message ********/
205 if (strlen($_POST['_draft_saveid']) > 3)
206 $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST);
208 $message_id = rcmail_gen_message_id();
210 // set default charset
211 $input_charset = $OUTPUT->get_charset();
212 $message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
214 $EMAIL_FORMAT_ERROR = NULL;
215 $RECIPIENT_COUNT = 0;
217 $mailto = rcmail_email_input_format(get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset), true);
218 $mailcc = rcmail_email_input_format(get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset), true);
219 $mailbcc = rcmail_email_input_format(get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset), true);
221 if ($EMAIL_FORMAT_ERROR) {
222 $OUTPUT->show_message('emailformaterror', 'error', array('email' => $EMAIL_FORMAT_ERROR));
223 $OUTPUT->send('iframe');
226 if (empty($mailto) && !empty($mailcc)) {
230 else if (empty($mailto))
231 $mailto = 'undisclosed-recipients:;';
233 // Get sender name and address...
234 $from = get_input_value('_from', RCUBE_INPUT_POST, true, $message_charset);
235 // ... from identity...
236 if (is_numeric($from)) {
237 if (is_array($identity_arr = rcmail_get_identity($from))) {
238 if ($identity_arr['mailto'])
239 $from = $identity_arr['mailto'];
240 if ($identity_arr['string'])
241 $from_string = $identity_arr['string'];
247 // ... if there is no identity record, this might be a custom from
248 else if ($from_string = rcmail_email_input_format($from)) {
249 if (preg_match('/(\S+@\S+)/', $from_string, $m))
250 $from = trim($m[1], '<>');
255 if (!$from_string && $from)
256 $from_string = $from;
258 // compose headers array
261 // if configured, the Received headers goes to top, for good measure
262 if ($CONFIG['http_received_header'])
266 $http_header = 'from ';
267 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
268 $host = $_SERVER['HTTP_X_FORWARDED_FOR'];
269 $hostname = gethostbyaddr($host);
270 if ($CONFIG['http_received_header_encrypt']) {
271 $http_header .= rcmail_encrypt_header($hostname);
272 if ($host != $hostname)
273 $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
275 $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
276 if ($host != $hostname)
277 $http_header .= ' (['. $host .'])';
279 $http_header .= $nldlm . ' via ';
281 $host = $_SERVER['REMOTE_ADDR'];
282 $hostname = gethostbyaddr($host);
283 if ($CONFIG['http_received_header_encrypt']) {
284 $http_header .= rcmail_encrypt_header($hostname);
285 if ($host != $hostname)
286 $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
288 $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
289 if ($host != $hostname)
290 $http_header .= ' (['. $host .'])';
293 $http_header .= $nldlm . 'by ' . $_SERVER['HTTP_HOST'];
295 $http_header .= $nldlm . 'with HTTP (' . $_SERVER['SERVER_PROTOCOL'] .
296 ' '.$_SERVER['REQUEST_METHOD'] . '); ' . date('r');
297 $http_header = wordwrap($http_header, 69, $nldlm);
299 $headers['Received'] = $http_header;
302 $headers['Date'] = rcmail_user_date();
303 $headers['From'] = rcube_charset_convert($from_string, RCMAIL_CHARSET, $message_charset);
304 $headers['To'] = $mailto;
306 // additional recipients
307 if (!empty($mailcc)) {
308 $headers['Cc'] = $mailcc;
310 if (!empty($mailbcc)) {
311 $headers['Bcc'] = $mailbcc;
313 if (!empty($identity_arr['bcc'])) {
314 $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
318 if (($max_recipients = (int) $RCMAIL->config->get('max_recipients')) > 0) {
319 if ($RECIPIENT_COUNT > $max_recipients) {
320 $OUTPUT->show_message('toomanyrecipients', 'error', array('max' => $max_recipients));
321 $OUTPUT->send('iframe');
326 $headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, TRUE, $message_charset));
328 if (!empty($identity_arr['organization'])) {
329 $headers['Organization'] = $identity_arr['organization'];
331 if (!empty($_POST['_replyto'])) {
332 $headers['Reply-To'] = rcmail_email_input_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
334 else if (!empty($identity_arr['reply-to'])) {
335 $headers['Reply-To'] = rcmail_email_input_format($identity_arr['reply-to'], false, true);
337 if (!empty($headers['Reply-To'])) {
338 $headers['Mail-Reply-To'] = $headers['Reply-To'];
340 if (!empty($_POST['_followupto'])) {
341 $headers['Mail-Followup-To'] = rcmail_email_input_format(get_input_value('_followupto', RCUBE_INPUT_POST, TRUE, $message_charset));
343 if (!empty($_SESSION['compose']['reply_msgid'])) {
344 $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
347 // remember reply/forward UIDs in special headers
348 if (!empty($_SESSION['compose']['reply_uid']) && $savedraft) {
349 $headers['X-Draft-Info'] = array('type' => 'reply', 'uid' => $_SESSION['compose']['reply_uid']);
351 else if (!empty($_SESSION['compose']['forward_uid']) && $savedraft) {
352 $headers['X-Draft-Info'] = array('type' => 'forward', 'uid' => $_SESSION['compose']['forward_uid']);
355 if (!empty($_SESSION['compose']['references'])) {
356 $headers['References'] = $_SESSION['compose']['references'];
359 if (!empty($_POST['_priority'])) {
360 $priority = intval($_POST['_priority']);
361 $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest');
362 if ($str_priority = $a_priorities[$priority]) {
363 $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
367 if (!empty($_POST['_receipt'])) {
368 $headers['Return-Receipt-To'] = $from_string;
369 $headers['Disposition-Notification-To'] = $from_string;
372 // additional headers
373 $headers['Message-ID'] = $message_id;
374 $headers['X-Sender'] = $from;
376 if (is_array($headers['X-Draft-Info'])) {
377 $headers['X-Draft-Info'] = rcmail_draftinfo_encode($headers['X-Draft-Info'] + array('folder' => $_SESSION['compose']['mailbox']));
379 if (!empty($CONFIG['useragent'])) {
380 $headers['User-Agent'] = $CONFIG['useragent'];
383 // exec hook for header checking and manipulation
384 $data = $RCMAIL->plugins->exec_hook('message_outgoing_headers', array('headers' => $headers));
386 // sending aborted by plugin
387 if ($data['abort'] && !$savedraft) {
388 $OUTPUT->show_message($data['message'] ? $data['message'] : 'sendingfailed');
389 $OUTPUT->send('iframe');
392 $headers = $data['headers'];
395 $isHtml = (bool) get_input_value('_is_html', RCUBE_INPUT_POST);
397 // fetch message body
398 $message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset);
402 // remove signature's div ID
403 $message_body = preg_replace('/\s*id="_rc_sig"/', '', $message_body);
405 // add inline css for blockquotes
406 $bstyle = 'padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%';
407 $message_body = preg_replace('/<blockquote>/',
408 '<blockquote type="cite" style="'.$bstyle.'">', $message_body);
410 // append doctype and html/body wrappers
411 $message_body = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">' .
412 "\r\n<html><body>\r\n" . $message_body;
415 // Check spelling before send
416 if ($CONFIG['spellcheck_before_send'] && $CONFIG['enable_spellcheck']
417 && empty($_SESSION['compose']['spell_checked']) && !empty($message_body)
419 $spellchecker = new rcube_spellchecker(get_input_value('_lang', RCUBE_INPUT_GPC));
420 $spell_result = $spellchecker->check($message_body, $isHtml);
422 $_SESSION['compose']['spell_checked'] = true;
424 if (!$spell_result) {
425 $result = $isHtml ? $spellchecker->get_words() : $spellchecker->get_xml();
426 $OUTPUT->show_message('mispellingsfound', 'error');
427 $OUTPUT->command('spellcheck_resume', $isHtml, $result);
428 $OUTPUT->send('iframe');
432 // generic footer for all messages
433 if ($isHtml && !empty($CONFIG['generic_message_footer_html'])) {
434 $footer = file_get_contents(realpath($CONFIG['generic_message_footer_html']));
435 $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
437 else if (!empty($CONFIG['generic_message_footer'])) {
438 $footer = file_get_contents(realpath($CONFIG['generic_message_footer']));
439 $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
441 $footer = '<pre>'.$footer.'</pre>';
445 $message_body .= "\r\n" . $footer;
447 $message_body .= "\r\n</body></html>\r\n";
450 // set line length for body wrapping
451 $LINE_LENGTH = $RCMAIL->config->get('line_length', 72);
453 // Since we can handle big messages with disk usage, we need more time to work
456 // create PEAR::Mail_mime instance
457 $MAIL_MIME = new Mail_mime("\r\n");
459 // Check if we have enough memory to handle the message in it
460 // It's faster than using files, so we'll do this if we only can
461 if (is_array($_SESSION['compose']['attachments']) && $CONFIG['smtp_server']
462 && ($mem_limit = parse_bytes(ini_get('memory_limit'))))
464 $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
466 foreach ($_SESSION['compose']['attachments'] as $id => $attachment)
467 $memory += $attachment['size'];
469 // Yeah, Net_SMTP needs up to 12x more memory, 1.33 is for base64
470 if ($memory * 1.33 * 12 > $mem_limit)
471 $MAIL_MIME->setParam('delay_file_io', true);
474 // For HTML-formatted messages, construct the MIME message with both
475 // the HTML part and the plain-text part
478 $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
479 array('body' => $message_body, 'type' => 'html', 'message' => $MAIL_MIME));
481 $MAIL_MIME->setHTMLBody($plugin['body']);
484 $plugin['body'] = rcmail_replace_emoticons($plugin['body']);
486 // add a plain text version of the e-mail as an alternative part.
487 $h2t = new html2text($plugin['body'], false, true, 0);
488 $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n");
489 $plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
490 if (!$plainTextPart) {
491 // empty message body breaks attachment handling in drafts
492 $plainTextPart = "\r\n";
495 // make sure all line endings are CRLF (#1486712)
496 $plainTextPart = preg_replace('/\r?\n/', "\r\n", $plainTextPart);
499 $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
500 array('body' => $plainTextPart, 'type' => 'alternative', 'message' => $MAIL_MIME));
502 $MAIL_MIME->setTXTBody($plugin['body']);
504 // look for "emoticon" images from TinyMCE and change their src paths to
505 // be file paths on the server instead of URL paths.
506 $message_body = rcmail_fix_emoticon_paths($MAIL_MIME);
509 $plugin = $RCMAIL->plugins->exec_hook('message_outgoing_body',
510 array('body' => $message_body, 'type' => 'plain', 'message' => $MAIL_MIME));
512 $message_body = $plugin['body'];
514 // compose format=flowed content if enabled
515 if ($flowed = $RCMAIL->config->get('send_format_flowed', true))
516 $message_body = rcube_message::format_flowed($message_body, min($LINE_LENGTH+2, 79));
518 $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n");
520 $message_body = wordwrap($message_body, 998, "\r\n", true);
521 if (!strlen($message_body)) {
522 // empty message body breaks attachment handling in drafts
523 $message_body = "\r\n";
526 $MAIL_MIME->setTXTBody($message_body, false, true);
529 // add stored attachments, if any
530 if (is_array($_SESSION['compose']['attachments']))
532 foreach ($_SESSION['compose']['attachments'] as $id => $attachment) {
533 // This hook retrieves the attachment contents from the file storage backend
534 $attachment = $RCMAIL->plugins->exec_hook('attachment_get', $attachment);
536 $dispurl = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]*/';
537 $message_body = $MAIL_MIME->getHTMLBody();
538 if ($isHtml && (preg_match($dispurl, $message_body) > 0)) {
539 $message_body = preg_replace($dispurl, ' src="'.$attachment['name'].'" ', $message_body);
540 $MAIL_MIME->setHTMLBody($message_body);
542 if ($attachment['data'])
543 $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false);
545 $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true);
548 $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914
549 $file = $attachment['data'] ? $attachment['data'] : $attachment['path'];
551 // .eml attachments send inline
552 $MAIL_MIME->addAttachment($file,
555 ($attachment['data'] ? false : true),
556 ($ctype == 'message/rfc822' ? '8bit' : 'base64'),
557 ($ctype == 'message/rfc822' ? 'inline' : 'attachment'),
559 $CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
560 $CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL,
567 // choose transfer encoding for plain/text body
568 if (preg_match('/[^\x00-\x7F]/', $MAIL_MIME->getTXTBody()))
569 $transfer_encoding = $RCMAIL->config->get('force_7bit') ? 'quoted-printable' : '8bit';
571 $transfer_encoding = '7bit';
573 // encoding settings for mail composing
574 $MAIL_MIME->setParam('text_encoding', $transfer_encoding);
575 $MAIL_MIME->setParam('html_encoding', 'quoted-printable');
576 $MAIL_MIME->setParam('head_encoding', 'quoted-printable');
577 $MAIL_MIME->setParam('head_charset', $message_charset);
578 $MAIL_MIME->setParam('html_charset', $message_charset);
579 $MAIL_MIME->setParam('text_charset', $message_charset . ($flowed ? ";\r\n format=flowed" : ''));
581 // encoding subject header with mb_encode provides better results with asian characters
582 if (function_exists('mb_encode_mimeheader')) {
583 mb_internal_encoding($message_charset);
584 $headers['Subject'] = mb_encode_mimeheader($headers['Subject'],
585 $message_charset, 'Q', "\r\n", 8);
586 mb_internal_encoding(RCMAIL_CHARSET);
589 // pass headers to message object
590 $MAIL_MIME->headers($headers);
592 // Begin SMTP Delivery Block
595 // check 'From' address (identity may be incomplete)
597 $OUTPUT->show_message('nofromaddress', 'error');
598 $OUTPUT->send('iframe');
601 // Handle Delivery Status Notification request
602 if (!empty($_POST['_dsn'])) {
603 $smtp_opts['dsn'] = true;
606 $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto,
607 $smtp_error, $mailbody_file, $smtp_opts);
609 // return to compose page if sending failed
613 if ($mailbody_file) {
614 unlink($mailbody_file);
618 $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']);
620 $OUTPUT->show_message('sendingfailed', 'error');
621 $OUTPUT->send('iframe');
624 // save message sent time
625 if (!empty($CONFIG['sendmail_delay']))
626 $RCMAIL->user->save_prefs(array('last_message_time' => time()));
628 // set replied/forwarded flag
629 if ($_SESSION['compose']['reply_uid'])
630 $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED', $_SESSION['compose']['mailbox']);
631 else if ($_SESSION['compose']['forward_uid'])
632 $IMAP->set_flag($_SESSION['compose']['forward_uid'], 'FORWARDED', $_SESSION['compose']['mailbox']);
634 } // End of SMTP Delivery Block
637 // Determine which folder to save message
639 $store_target = $CONFIG['drafts_mbox'];
641 $store_target = isset($_POST['_store_target']) ? get_input_value('_store_target', RCUBE_INPUT_POST) : $CONFIG['sent_mbox'];
645 // check if folder is subscribed
646 if ($IMAP->mailbox_exists($store_target, true))
647 $store_folder = true;
648 // folder may be existing but not subscribed (#1485241)
649 else if (!$IMAP->mailbox_exists($store_target))
650 $store_folder = $IMAP->create_mailbox($store_target, true);
651 else if ($IMAP->subscribe($store_target))
652 $store_folder = true;
654 // append message to sent box
657 // message body in file
658 if ($mailbody_file || $MAIL_MIME->getParam('delay_file_io')) {
659 $headers = $MAIL_MIME->txtHeaders();
661 // file already created
663 $msg = $mailbody_file;
665 $temp_dir = $RCMAIL->config->get('temp_dir');
666 $mailbody_file = tempnam($temp_dir, 'rcmMsg');
667 if (!PEAR::isError($msg = $MAIL_MIME->saveMessageBody($mailbody_file)))
668 $msg = $mailbody_file;
672 $msg = $MAIL_MIME->getMessage();
676 if (PEAR::isError($msg))
677 raise_error(array('code' => 650, 'type' => 'php',
678 'file' => __FILE__, 'line' => __LINE__,
679 'message' => "Could not create message: ".$msg->getMessage()),
682 $saved = $IMAP->save_message($store_target, $msg, $headers, $mailbody_file ? true : false);
685 if ($mailbody_file) {
686 unlink($mailbody_file);
687 $mailbody_file = null;
690 // raise error if saving failed
692 raise_error(array('code' => 800, 'type' => 'imap',
693 'file' => __FILE__, 'line' => __LINE__,
694 'message' => "Could not save message in $store_target"), TRUE, FALSE);
697 $OUTPUT->show_message('errorsaving', 'error');
698 $OUTPUT->send('iframe');
703 if ($olddraftmessageid)
705 // delete previous saved draft
706 $a_deleteid = $IMAP->search_once($CONFIG['drafts_mbox'],
707 'HEADER Message-ID '.$olddraftmessageid, true);
708 $deleted = $IMAP->delete_message($a_deleteid, $CONFIG['drafts_mbox']);
710 // raise error if deletion of old draft failed
712 raise_error(array('code' => 800, 'type' => 'imap',
713 'file' => __FILE__, 'line' => __LINE__,
714 'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE);
718 else if ($mailbody_file) {
719 unlink($mailbody_file);
725 $msgid = strtr($message_id, array('>' => '', '<' => ''));
727 // remember new draft-uid
728 $draftuids = $IMAP->search_once($CONFIG['drafts_mbox'], 'HEADER Message-ID '.$msgid, true);
729 $_SESSION['compose']['param']['draft_uid'] = $draftuids[0];
732 $OUTPUT->show_message('messagesaved', 'confirmation');
734 // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
735 $OUTPUT->command('set_draft_id', $msgid);
736 $OUTPUT->command('compose_field_hash', true);
738 // start the auto-save timer again
739 $OUTPUT->command('auto_save_start');
741 $OUTPUT->send('iframe');
745 rcmail_compose_cleanup($COMPOSE_ID);
747 if ($store_folder && !$saved)
748 $OUTPUT->command('sent_successfully', 'error', rcube_label('errorsavingsent'));
750 $OUTPUT->command('sent_successfully', 'confirmation', rcube_label('messagesent'));
751 $OUTPUT->send('iframe');