Index: class/deliver/Deliver.class.php
===================================================================
--- class/deliver/Deliver.class.php	(revision 12829)
+++ class/deliver/Deliver.class.php	(working copy)
@@ -59,14 +65,18 @@
         // calculate reply header if needed
         //
         if ($reply_id) {
-            global $imapConnection, $username, $key, $imapServerAddress, 
-                   $imapPort, $mailbox;
-            if (!$imapConnection)
-                $imapConnection = sqimap_login($username, $key, 
-                                               $imapServerAddress, $imapPort, 0);
+            global $username, $key, $imapServerAddress, $imapPort, $mailbox;
 
-            sqimap_mailbox_select($imapConnection, $mailbox);
-            $reply_message = sqimap_get_message($imapConnection, $reply_id, $mailbox);
+            // use our own IMAP connection in case the global one
+            // has selected a different folder, initiated an APPEND
+            // (as in the case of copying sent messages to the Sent
+            // folder), etc
+            //
+            $myImapConnection = sqimap_login($username, $key, 
+                                             $imapServerAddress, $imapPort, 0);
+            sqimap_mailbox_select($myImapConnection, $mailbox);
+            $reply_message = sqimap_get_message($myImapConnection, $reply_id, $mailbox);
+            sqimap_logout($myImapConnection);
 
             if ($reply_ent_id) {
                 /* redefine the messsage in case of message/rfc822 */
