source: github/program/steps/mail/sendmail.inc @ 91790e4

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 91790e4 was 91790e4, checked in by alecpl <alec@…>, 3 years ago
  • Fix attachment excessive memory use, support messages of any size (#1484660)
  • Property mode set to 100644
File size: 21.5 KB
Line 
1<?php
2
3/*
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/sendmail.inc                                       |
6 |                                                                       |
7 | This file is part of the RoundCube Webmail client                     |
8 | Copyright (C) 2005-2009, RoundCube Dev. - Switzerland                 |
9 | Licensed under the GNU GPL                                            |
10 |                                                                       |
11 | PURPOSE:                                                              |
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()       |
14 |                                                                       |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
17 +-----------------------------------------------------------------------+
18
19 $Id$
20
21*/
22
23// remove all scripts and act as called in frame
24$OUTPUT->reset();
25$OUTPUT->framed = TRUE;
26
27$savedraft = !empty($_POST['_draft']) ? TRUE : FALSE;
28
29/****** checks ********/
30
31if (!isset($_SESSION['compose']['id'])) {
32  raise_error(array('code' => 500, 'type' => 'smtp',
33    'file' => __FILE__, 'line' => __LINE__,
34    'message' => "Invalid compose ID"), true, false);
35
36  $OUTPUT->show_message("An internal error occured. Please try again.", 'error');
37  $OUTPUT->send('iframe');
38}
39
40if (!$savedraft) {
41  if (empty($_POST['_to']) && empty($_POST['_cc']) && empty($_POST['_bcc'])
42    && empty($_POST['_subject']) && $_POST['_message']) {
43    $OUTPUT->show_message('sendingfailed', 'error');
44    $OUTPUT->send('iframe');
45  }
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');
52    }
53  }
54}
55
56
57/****** message sending functions ********/
58
59// encrypt parts of the header
60function rcmail_encrypt_header($what)
61{
62  global $CONFIG, $RCMAIL;
63  if (!$CONFIG['http_received_header_encrypt'])
64  {
65    return $what;
66  }
67  return $RCMAIL->encrypt($what);
68}
69
70// get identity record
71function rcmail_get_identity($id)
72  {
73  global $USER, $OUTPUT;
74 
75  if ($sql_arr = $USER->get_identity($id))
76    {
77    $out = $sql_arr;
78    $out['mailto'] = $sql_arr['email'];
79   
80    // Special chars as defined by RFC 822 need to in quoted string (or escaped).
81    if (preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $sql_arr['name']))
82      $name = '"' . addcslashes($sql_arr['name'], '"') . '"';
83    else
84      $name = $sql_arr['name'];
85
86    $out['string'] = rcube_charset_convert($name, RCMAIL_CHARSET, $OUTPUT->get_charset());
87    if ($sql_arr['email'])
88      $out['string'] .= ' <' . $sql_arr['email'] . '>';
89
90    return $out;
91    }
92
93  return FALSE; 
94  }
95
96/**
97 * go from this:
98 * <img src=".../tiny_mce/plugins/emotions/images/smiley-cool.gif" border="0" alt="Cool" title="Cool" />
99 *
100 * to this:
101 *
102 * <IMG src="cid:smiley-cool.gif"/>
103 * ...
104 * ------part...
105 * Content-Type: image/gif
106 * Content-Transfer-Encoding: base64
107 * Content-ID: <smiley-cool.gif>
108 */
109function rcmail_attach_emoticons(&$mime_message)
110{
111  global $CONFIG;
112
113  $body = $mime_message->getHTMLBody();
114
115  // remove any null-byte characters before parsing
116  $body = preg_replace('/\x00/', '', $body);
117 
118  $searchstr = 'program/js/tiny_mce/plugins/emotions/img/';
119  $offset = 0;
120
121  // keep track of added images, so they're only added once
122  $included_images = array();
123
124  if (preg_match_all('# src=[\'"]([^\'"]+)#', $body, $matches, PREG_OFFSET_CAPTURE)) {
125    foreach ($matches[1] as $m) {
126      // find emoticon image tags
127      if (preg_match('#'.$searchstr.'(.*)$#', $m[0], $imatches)) {
128        $image_name = $imatches[1];
129
130        // sanitize image name so resulting attachment doesn't leave images dir
131        $image_name = preg_replace('/[^a-zA-Z0-9_\.\-]/i', '', $image_name);
132        $img_file = INSTALL_PATH . '/' . $searchstr . $image_name;
133
134        if (! in_array($image_name, $included_images)) {
135          // add the image to the MIME message
136          if (! $mime_message->addHTMLImage($img_file, 'image/gif', '', true, $image_name))
137            $OUTPUT->show_message("emoticonerror", 'error');
138          array_push($included_images, $image_name);
139        }
140
141        $body = substr_replace($body, $img_file, $m[1] + $offset, strlen($m[0]));
142        $offset += strlen($img_file) - strlen($m[0]);
143      }
144    }
145  }
146
147  $mime_message->setHTMLBody($body);
148
149  return $body;
150}
151
152// parse email address input
153function rcmail_email_input_format($mailto)
154{
155  global $EMAIL_FORMAT_ERROR;
156
157  $regexp = array('/[,;]\s*[\r\n]+/', '/[\r\n]+/', '/[,;]\s*$/m', '/;/', '/(\S{1})(<\S+@\S+>)/U');
158  $replace = array(', ', ', ', '', ',', '\\1 \\2');
159
160  // replace new lines and strip ending ', ', make address input more valid
161  $mailto = trim(preg_replace($regexp, $replace, $mailto));
162
163  $result = array();
164  $items = rcube_explode_quoted_string(',', $mailto);
165
166  foreach($items as $item) {
167    $item = trim($item);
168    // address in brackets without name (do nothing)
169    if (preg_match('/^<\S+@\S+>$/', $item)) {
170      $result[] = $item;
171    // address without brackets and without name (add brackets)
172    } else if (preg_match('/^\S+@\S+$/', $item)) {
173      $result[] = '<'.$item.'>';
174    // address with name (handle name)
175    } else if (preg_match('/\S+@\S+>*$/', $item, $matches)) {
176      $address = $matches[0];
177      $name = str_replace($address, '', $item);
178      $name = trim($name);
179      if ($name && ($name[0] != '"' || $name[strlen($name)-1] != '"')
180          && preg_match('/[\(\)\<\>\\\.\[\]@,;:"]/', $name)) {
181          $name = '"'.addcslashes($name, '"').'"';
182      }
183      if (!preg_match('/^<\S+@\S+>$/', $address))
184        $address = '<'.$address.'>';
185
186      $result[] = $name.' '.$address;
187      $item = $address;
188    } else if (trim($item)) {
189      continue;
190    }
191
192    // check address format
193    $item = trim($item, '<>');
194    if ($item && !check_email($item)) {
195      $EMAIL_FORMAT_ERROR = $item;
196      return;
197    }
198  }
199
200  return implode(', ', $result);
201}
202
203/****** compose message ********/
204
205if (strlen($_POST['_draft_saveid']) > 3)
206  $olddraftmessageid = get_input_value('_draft_saveid', RCUBE_INPUT_POST);
207
208$message_id = sprintf('<%s@%s>', md5(uniqid('rcmail'.mt_rand(),true)), $RCMAIL->config->mail_domain($_SESSION['imap_host']));
209
210// set default charset
211$input_charset = $OUTPUT->get_charset();
212$message_charset = isset($_POST['_charset']) ? $_POST['_charset'] : $input_charset;
213
214$EMAIL_FORMAT_ERROR = NULL;
215
216$mailto = rcmail_email_input_format(get_input_value('_to', RCUBE_INPUT_POST, TRUE, $message_charset));
217$mailcc = rcmail_email_input_format(get_input_value('_cc', RCUBE_INPUT_POST, TRUE, $message_charset));
218$mailbcc = rcmail_email_input_format(get_input_value('_bcc', RCUBE_INPUT_POST, TRUE, $message_charset));
219
220if ($EMAIL_FORMAT_ERROR) {
221  $OUTPUT->show_message('emailformaterror', 'error', array('email' => $EMAIL_FORMAT_ERROR));
222  $OUTPUT->send('iframe');
223}
224
225if (empty($mailto) && !empty($mailcc)) {
226  $mailto = $mailcc;
227  $mailcc = null;
228}
229else if (empty($mailto))
230  $mailto = 'undisclosed-recipients:;';
231
232// get sender name and address
233$from = get_input_value('_from', RCUBE_INPUT_POST, true, $message_charset);
234$identity_arr = rcmail_get_identity($from);
235
236if (!$identity_arr && ($from = rcmail_email_input_format($from))) {
237  if (preg_match('/(\S+@\S+)/', $from, $m))
238    $identity_arr['mailto'] = $m[1];
239} else
240  $from = $identity_arr['mailto'];
241
242if (empty($identity_arr['string']))
243  $identity_arr['string'] = $from;
244
245// compose headers array
246$headers = array();
247
248// if configured, the Received headers goes to top, for good measure
249if ($CONFIG['http_received_header'])
250{
251  $nldlm = $RCMAIL->config->header_delimiter() . "\t";
252  // FROM/VIA
253  $http_header = 'from ';
254  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
255    $host = $_SERVER['HTTP_X_FORWARDED_FOR'];
256    $hostname = gethostbyaddr($host);
257    if ($CONFIG['http_received_header_encrypt']) {
258      $http_header .= rcmail_encrypt_header($hostname);
259      if ($host != $hostname)
260        $http_header .= ' ('. rcmail_encrypt_header($host) . ')';
261    } else {
262      $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
263      $http_header .= ' ('. ($host == $hostname ? '' : $hostname . ' ') .
264        '[' . $host .'])';
265    }
266    $http_header .= $nldlm . ' via ';
267  }
268  $host = $_SERVER['REMOTE_ADDR'];
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) . ')';
274  } else {
275    $http_header .= (($host != $hostname) ? $hostname : '[' . $host . ']');
276    $http_header .= ' ('. ($host == $hostname ? '' : $hostname . ' ') .
277      '[' . $host .'])';
278  }
279  // BY
280  $http_header .= $nldlm . 'by ' . $_SERVER['HTTP_HOST'];
281  // WITH
282  $http_header .= $nldlm . 'with ' . $_SERVER['SERVER_PROTOCOL'] .
283      ' ('.$_SERVER['REQUEST_METHOD'] . '); ' . date('r');
284  $http_header = wordwrap($http_header, 69, $nldlm);
285
286  $headers['Received'] = $http_header;
287}
288
289$headers['Date'] = date('r');
290$headers['From'] = rcube_charset_convert($identity_arr['string'], RCMAIL_CHARSET, $message_charset);
291$headers['To'] = $mailto;
292
293// additional recipients
294if (!empty($mailcc))
295  $headers['Cc'] = $mailcc;
296
297if (!empty($mailbcc))
298  $headers['Bcc'] = $mailbcc;
299 
300if (!empty($identity_arr['bcc']))
301  $headers['Bcc'] = ($headers['Bcc'] ? $headers['Bcc'].', ' : '') . $identity_arr['bcc'];
302
303// add subject
304$headers['Subject'] = trim(get_input_value('_subject', RCUBE_INPUT_POST, TRUE, $message_charset));
305
306if (!empty($identity_arr['organization']))
307  $headers['Organization'] = $identity_arr['organization'];
308
309if (!empty($_POST['_replyto']))
310  $headers['Reply-To'] = rcmail_email_input_format(get_input_value('_replyto', RCUBE_INPUT_POST, TRUE, $message_charset));
311else if (!empty($identity_arr['reply-to']))
312  $headers['Reply-To'] = $identity_arr['reply-to'];
313
314if (!empty($_SESSION['compose']['reply_msgid']))
315  $headers['In-Reply-To'] = $_SESSION['compose']['reply_msgid'];
316
317if (!empty($_SESSION['compose']['references']))
318  $headers['References'] = $_SESSION['compose']['references'];
319
320if (!empty($_POST['_priority']))
321  {
322  $priority = intval($_POST['_priority']);
323  $a_priorities = array(1=>'highest', 2=>'high', 4=>'low', 5=>'lowest');
324  if ($str_priority = $a_priorities[$priority])
325    $headers['X-Priority'] = sprintf("%d (%s)", $priority, ucfirst($str_priority));
326  }
327
328if (!empty($_POST['_receipt']))
329  {
330  $headers['Return-Receipt-To'] = $identity_arr['string'];
331  $headers['Disposition-Notification-To'] = $identity_arr['string'];
332  }
333
334// additional headers
335$headers['Message-ID'] = $message_id;
336$headers['X-Sender'] = $from;
337
338if (!empty($CONFIG['useragent']))
339  $headers['User-Agent'] = $CONFIG['useragent'];
340
341$isHtmlVal = strtolower(get_input_value('_is_html', RCUBE_INPUT_POST));
342$isHtml = ($isHtmlVal == "1");
343
344// fetch message body
345$message_body = get_input_value('_message', RCUBE_INPUT_POST, TRUE, $message_charset);
346
347if (!$savedraft) {
348  // remove signature's div ID
349  if ($isHtml)
350    $message_body = preg_replace('/\s*id="_rc_sig"/', '', $message_body);
351
352  // generic footer for all messages
353  if (!empty($CONFIG['generic_message_footer'])) {
354    $footer = file_get_contents(realpath($CONFIG['generic_message_footer']));
355    $footer = rcube_charset_convert($footer, RCMAIL_CHARSET, $message_charset);
356  }
357}
358
359// set line length for body wrapping
360$LINE_LENGTH = $RCMAIL->config->get('line_length', 75);
361
362// Since we can handle big messages with disk usage, we need more time to work
363@set_time_limit(0);
364
365// create PEAR::Mail_mime instance
366$MAIL_MIME = new Mail_mime($RCMAIL->config->header_delimiter());
367
368// Check if we have enough memory to handle the message in it
369// It's faster than using files, so we'll do this if we only can
370if (is_array($_SESSION['compose']['attachments']) && $CONFIG['smtp_server']
371  && ($mem_limit = parse_bytes(ini_get('memory_limit'))))
372{
373  $memory = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB
374
375  foreach ($_SESSION['compose']['attachments'] as $id => $attachment)
376    $memory += $attachment['size'];
377
378  // Yeah, Net_SMTP needs up to 12x more memory, 1.33 is for base64
379  if ($memory * 1.33 * 12 > $mem_limit)
380    $MAIL_MIME->setParam('delay_file_io', true);
381}
382
383// For HTML-formatted messages, construct the MIME message with both
384// the HTML part and the plain-text part
385
386if ($isHtml) {
387  $plugin = $RCMAIL->plugins->exec_hook('outgoing_message_body', array('body' => $message_body, 'type' => 'html', 'message' => $MAIL_MIME));
388  $MAIL_MIME->setHTMLBody($plugin['body'] . ($footer ? "\r\n<pre>".$footer.'</pre>' : ''));
389
390  // add a plain text version of the e-mail as an alternative part.
391  $h2t = new html2text($plugin['body'], false, true, 0);
392  $plainTextPart = rc_wordwrap($h2t->get_text(), $LINE_LENGTH, "\r\n") . ($footer ? "\r\n".$footer : '');
393  $plainTextPart = wordwrap($plainTextPart, 998, "\r\n", true);
394  if (!strlen($plainTextPart)) {
395    // empty message body breaks attachment handling in drafts
396    $plainTextPart = "\r\n";
397  }
398  $plugin = $RCMAIL->plugins->exec_hook('outgoing_message_body', array('body' => $plainTextPart, 'type' => 'alternative', 'message' => $MAIL_MIME));
399  $MAIL_MIME->setTXTBody($plugin['body']);
400
401  // look for "emoticon" images from TinyMCE and copy into message as attachments
402  $message_body = rcmail_attach_emoticons($MAIL_MIME);
403}
404else
405  {
406  $message_body = rc_wordwrap($message_body, $LINE_LENGTH, "\r\n");
407  if ($footer)
408    $message_body .= "\r\n" . $footer;
409  $message_body = wordwrap($message_body, 998, "\r\n", true);
410  if (!strlen($message_body)) {
411    // empty message body breaks attachment handling in drafts
412    $message_body = "\r\n";
413  }
414  $plugin = $RCMAIL->plugins->exec_hook('outgoing_message_body', array('body' => $message_body, 'type' => 'plain', 'message' => $MAIL_MIME));
415  $MAIL_MIME->setTXTBody($plugin['body'], false, true);
416}
417
418// chose transfer encoding
419$charset_7bit = array('ASCII', 'ISO-2022-JP', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-15');
420$transfer_encoding = in_array(strtoupper($message_charset), $charset_7bit) ? '7bit' : '8bit';
421
422// add stored attachments, if any
423if (is_array($_SESSION['compose']['attachments']))
424{
425  foreach ($_SESSION['compose']['attachments'] as $id => $attachment) {
426    // This hook retrieves the attachment contents from the file storage backend
427    $attachment = $RCMAIL->plugins->exec_hook('get_attachment', $attachment);
428
429    $dispurl = '/\ssrc\s*=\s*[\'"]*\S+display-attachment\S+file=rcmfile' . preg_quote($attachment['id']) . '[\s\'"]\s*/';
430    $message_body = $MAIL_MIME->getHTMLBody();
431    if ($isHtml && (preg_match($dispurl, $message_body) > 0)) {
432      $message_body = preg_replace($dispurl, ' src="'.$attachment['name'].'" ', $message_body);
433      $MAIL_MIME->setHTMLBody($message_body);
434     
435      if ($attachment['data'])
436        $MAIL_MIME->addHTMLImage($attachment['data'], $attachment['mimetype'], $attachment['name'], false);
437      else
438        $MAIL_MIME->addHTMLImage($attachment['path'], $attachment['mimetype'], $attachment['name'], true);
439    }
440    else {
441      $ctype = str_replace('image/pjpeg', 'image/jpeg', $attachment['mimetype']); // #1484914
442      $file = $attachment['data'] ? $attachment['data'] : $attachment['path'];
443
444      // .eml attachments send inline
445      $MAIL_MIME->addAttachment($file,
446        $ctype,
447        $attachment['name'],
448        ($attachment['data'] ? false : true),
449        ($ctype == 'message/rfc822' ? $transfer_encoding : 'base64'),
450        ($ctype == 'message/rfc822' ? 'inline' : 'attachment'),
451        $message_charset, '', '',
452        $CONFIG['mime_param_folding'] ? 'quoted-printable' : NULL,
453        $CONFIG['mime_param_folding'] == 2 ? 'quoted-printable' : NULL
454      );
455    }
456  }
457}
458
459// encoding settings for mail composing
460$MAIL_MIME->setParam('text_encoding', $transfer_encoding);
461$MAIL_MIME->setParam('html_encoding', 'quoted-printable');
462$MAIL_MIME->setParam('head_encoding', 'quoted-printable');
463$MAIL_MIME->setParam('head_charset', $message_charset);
464$MAIL_MIME->setParam('html_charset', $message_charset);
465$MAIL_MIME->setParam('text_charset', $message_charset);
466
467$data = $RCMAIL->plugins->exec_hook('outgoing_message_headers', array('headers' => $headers));
468$headers = $data['headers'];
469
470// encoding subject header with mb_encode provides better results with asian characters
471if (function_exists('mb_encode_mimeheader'))
472{
473  mb_internal_encoding($message_charset);
474  $headers['Subject'] = mb_encode_mimeheader($headers['Subject'],
475    $message_charset, 'Q', $RCMAIL->config->header_delimiter(), 8);
476  mb_internal_encoding(RCMAIL_CHARSET);
477}
478
479// pass headers to message object
480$MAIL_MIME->headers($headers);
481
482// Begin SMTP Delivery Block
483if (!$savedraft)
484{
485  // check for 'From' address (identity may be incomplete)
486  if ($identity_arr && !$identity_arr['mailto']) {
487    $OUTPUT->show_message('nofromaddress', 'error');
488    $OUTPUT->send('iframe');
489  }
490
491  $sent = rcmail_deliver_message($MAIL_MIME, $from, $mailto, $smtp_error, $mailbody_file);
492
493  // return to compose page if sending failed
494  if (!$sent)
495    {
496    // remove temp file
497    if ($mailbody_file) {
498      unlink($mailbody_file);
499      }
500
501    if ($smtp_error)
502      $OUTPUT->show_message($smtp_error['label'], 'error', $smtp_error['vars']);
503    else
504      $OUTPUT->show_message('sendingfailed', 'error');
505    $OUTPUT->send('iframe');
506    }
507
508  // save message sent time
509  if (!empty($CONFIG['sendmail_delay']))
510    $RCMAIL->user->save_prefs(array('last_message_time' => time()));
511 
512  // set replied/forwarded flag
513  if ($_SESSION['compose']['reply_uid'])
514    $IMAP->set_flag($_SESSION['compose']['reply_uid'], 'ANSWERED', $_SESSION['compose']['mailbox']);
515  else if ($_SESSION['compose']['forward_uid'])
516    $IMAP->set_flag($_SESSION['compose']['forward_uid'], 'FORWARDED', $_SESSION['compose']['mailbox']);
517
518} // End of SMTP Delivery Block
519
520
521// Determine which folder to save message
522if ($savedraft)
523  $store_target = $CONFIG['drafts_mbox'];
524else   
525  $store_target = isset($_POST['_store_target']) ? get_input_value('_store_target', RCUBE_INPUT_POST) : $CONFIG['sent_mbox'];
526
527if ($store_target)
528  {
529  // check if mailbox exists
530  if (!in_array($store_target, $IMAP->list_mailboxes()))
531    {
532      // folder may be existing but not subscribed (#1485241)
533      if (!in_array($store_target, $IMAP->list_unsubscribed()))
534        $store_folder = $IMAP->create_mailbox($store_target, TRUE);
535      else if ($IMAP->subscribe($store_target))
536        $store_folder = TRUE;
537    }
538  else
539    $store_folder = TRUE;
540
541  // append message to sent box
542  if ($store_folder) {
543
544    // message body in file
545    if ($mailbody_file || $MAIL_MIME->getParam('delay_file_io')) {
546      $headers = $MAIL_MIME->txtHeaders();
547     
548      // file already created
549      if ($mailbody_file)
550        $msg = $mailbody_file;
551      else {
552        $temp_dir = $RCMAIL->config->get('temp_dir');
553        $mailbody_file = tempnam($temp_dir, 'rcmMsg');
554        if (!PEAR::isError($msg = $MAIL_MIME->saveMessageBody($mailbody_file)))
555          $msg = $mailbody_file;
556        }
557      }
558    else {
559      $msg = $MAIL_MIME->getMessage();
560      $headers = '';
561      }
562
563    if (PEAR::isError($msg))
564      raise_error(array('code' => 600, 'type' => 'php',
565            'file' => __FILE__, 'line' => __LINE__,
566            'message' => "Could not create message: ".$msg->getMessage()),
567            TRUE, FALSE);
568    else {
569      $saved = $IMAP->save_message($store_target, $msg, $headers, $mailbody_file ? true : false);
570      }
571
572    if ($mailbody_file) {
573      unlink($mailbody_file);
574      $mailbody_file = null;
575      }
576
577    // raise error if saving failed
578    if (!$saved) {
579      raise_error(array('code' => 800, 'type' => 'imap',
580            'file' => __FILE__, 'line' => __LINE__,
581            'message' => "Could not save message in $store_target"), TRUE, FALSE);
582   
583      if ($savedraft) {
584        $OUTPUT->show_message('errorsaving', 'error');
585        $OUTPUT->send('iframe');
586        }
587      }
588    }
589
590  if ($olddraftmessageid)
591    {
592    // delete previous saved draft
593    $a_deleteid = $IMAP->search($CONFIG['drafts_mbox'], 'HEADER Message-ID '.$olddraftmessageid);
594
595    $deleted = $IMAP->delete_message($IMAP->get_uid($a_deleteid[0], $CONFIG['drafts_mbox']), $CONFIG['drafts_mbox']);
596
597    // raise error if deletion of old draft failed
598    if (!$deleted)
599      raise_error(array('code' => 800, 'type' => 'imap',
600                'file' => __FILE__, 'line' => __LINE__,
601                'message' => "Could not delete message from ".$CONFIG['drafts_mbox']), TRUE, FALSE);
602    }
603  }
604// remove temp file
605else if ($mailbody_file) {
606  unlink($mailbody_file);
607  }
608
609
610if ($savedraft)
611  {
612  $msgid = strtr($message_id, array('>' => '', '<' => ''));
613 
614  // remember new draft-uid
615  $draftids = $IMAP->search($CONFIG['drafts_mbox'], 'HEADER Message-ID '.$msgid);
616  $_SESSION['compose']['param']['_draft_uid'] = $IMAP->get_uid($draftids[0], $CONFIG['drafts_mbox']);
617
618  // display success
619  $OUTPUT->show_message('messagesaved', 'confirmation');
620
621  // update "_draft_saveid" and the "cmp_hash" to prevent "Unsaved changes" warning
622  $OUTPUT->command('set_draft_id', $msgid);
623  $OUTPUT->command('compose_field_hash', true);
624
625  // start the auto-save timer again
626  $OUTPUT->command('auto_save_start');
627
628  $OUTPUT->send('iframe');
629  }
630else
631  {
632  rcmail_compose_cleanup();
633
634  if ($store_folder && !$saved)
635    $OUTPUT->command('sent_successfully', 'error', rcube_label('errorsavingsent'));
636  else
637    $OUTPUT->command('sent_successfully', 'confirmation', rcube_label('messagesent'));
638  $OUTPUT->send('iframe');
639  }
640
641?>
Note: See TracBrowser for help on using the repository browser.