From 4b72a1f49843aa64cdf90301ae71035c3e6cf30a Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Fri, 31 Jul 2015 12:48:17 -0400
Subject: [PATCH] Fix error when using back button after sending an email (#1490009)

---
 program/steps/mail/sendmail.inc |   75 ++++++++++++++++++++++---------------
 1 files changed, 44 insertions(+), 31 deletions(-)

diff --git a/program/steps/mail/sendmail.inc b/program/steps/mail/sendmail.inc
index e90b0ef..d83f26e 100644
--- a/program/steps/mail/sendmail.inc
+++ b/program/steps/mail/sendmail.inc
@@ -1,6 +1,6 @@
 <?php
 
-/*
+/**
  +-----------------------------------------------------------------------+
  | program/steps/mail/sendmail.inc                                       |
  |                                                                       |
@@ -111,6 +111,12 @@
         $from = trim($m[1], '<>');
     else
         $from = null;
+}
+
+// check 'From' address (identity may be incomplete)
+if (!$savedraft && !$saveonly && empty($from)) {
+    $OUTPUT->show_message('nofromaddress', 'error');
+    $OUTPUT->send('iframe');
 }
 
 if (!$from_string && $from) {
@@ -505,25 +511,15 @@
 $MAIL_MIME->setParam('html_charset', $message_charset);
 $MAIL_MIME->setParam('text_charset', $text_charset);
 
-// encoding subject header with mb_encode provides better results with asian characters
-if (function_exists('mb_encode_mimeheader')) {
-    mb_internal_encoding($message_charset);
-    $headers['Subject'] = mb_encode_mimeheader($headers['Subject'],
-        $message_charset, 'Q', "\r\n", 8);
-    mb_internal_encoding(RCUBE_CHARSET);
-}
-
 // pass headers to message object
 $MAIL_MIME->headers($headers);
 
-// Begin SMTP Delivery Block
-if (!$savedraft) {
-    // check 'From' address (identity may be incomplete)
-    if (empty($from)) {
-        $OUTPUT->show_message('nofromaddress', 'error');
-        $OUTPUT->send('iframe');
-    }
+// This hook allows to modify the message before send or save action
+$plugin    = $RCMAIL->plugins->exec_hook('message_ready', array('message' => $MAIL_MIME));
+$MAIL_MIME = $plugin['message'];
 
+// Begin SMTP Delivery Block
+if (!$savedraft && !$saveonly) {
     // Handle Delivery Status Notification request
     $smtp_opts['dsn'] = $dsn_enabled;
 
@@ -601,8 +597,9 @@
             else {
                 $temp_dir      = $RCMAIL->config->get('temp_dir');
                 $mailbody_file = tempnam($temp_dir, 'rcmMsg');
+                $msg           = $MAIL_MIME->saveMessageBody($mailbody_file);
 
-                if (!PEAR::isError($msg = $MAIL_MIME->saveMessageBody($mailbody_file))) {
+                if (!is_a($msg, 'PEAR_Error')) {
                     $msg = $mailbody_file;
                 }
             }
@@ -612,7 +609,7 @@
             $headers = '';
         }
 
-        if (PEAR::isError($msg)) {
+        if (is_a($msg, 'PEAR_Error')) {
             rcube::raise_error(array('code' => 650, 'type' => 'php',
                 'file' => __FILE__, 'line' => __LINE__,
                 'message' => "Could not create message: ".$msg->getMessage()),
@@ -643,24 +640,24 @@
             $OUTPUT->send('iframe');
         }
     }
-
-    // delete previous saved draft
-    if ($saved && ($old_id = rcube_utils::get_input_value('_draft_saveid', rcube_utils::INPUT_POST))) {
-        $deleted = $RCMAIL->storage->delete_message($old_id, $drafts_mbox);
-
-        // raise error if deletion of old draft failed
-        if (!$deleted) {
-            rcube::raise_error(array('code' => 800, 'type' => 'imap',
-                'file' => __FILE__, 'line' => __LINE__,
-                'message' => "Could not delete message from $drafts_mbox"), true, false);
-        }
-    }
 }
 // remove temp file
 else if ($mailbody_file) {
     unlink($mailbody_file);
 }
 
+// delete previous saved draft
+$old_id = rcube_utils::get_input_value('_draft_saveid', rcube_utils::INPUT_POST);
+if ($old_id && ($sent || $saved)) {
+    $deleted = $RCMAIL->storage->delete_message($old_id, $drafts_mbox);
+
+    // raise error if deletion of old draft failed
+    if (!$deleted) {
+        rcube::raise_error(array('code' => 800, 'type' => 'imap',
+            'file' => __FILE__, 'line' => __LINE__,
+            'message' => "Could not delete message from $drafts_mbox"), true, false);
+    }
+}
 
 if ($savedraft) {
     // remember new draft-uid ($saved could be an UID or true/false here)
@@ -800,7 +797,7 @@
                 if (!in_array($image_name, $included_images)) {
                     // add the image to the MIME message
                     $res = $mime_message->addHTMLImage($img_file, 'image/gif', '', true, $image_name);
-                    if (PEAR::isError($res)) {
+                    if (is_a($res, 'PEAR_Error')) {
                         $RCMAIL->output->show_message("emoticonerror", 'error');
                         continue;
                     }
@@ -961,3 +958,19 @@
 
     return false;
 }
+
+/**
+ * clear message composing settings
+ */
+function rcmail_compose_cleanup($id)
+{
+    if (!isset($_SESSION['compose_data_'.$id])) {
+        return;
+    }
+
+    $rcmail = rcmail::get_instance();
+    $rcmail->plugins->exec_hook('attachments_cleanup', array('group' => $id));
+    $rcmail->session->remove('compose_data_'.$id);
+
+    $_SESSION['last_compose_session'] = $id;
+}

--
Gitblit v1.9.1