Change order of arguments
[squirrelmail.git] / class / deliver / Deliver.class.php
index ad4e1fd79544f964573571dedfa595011a7fa9d6..11a8e138014feb2989d585396e150d9d8c201f16 100644 (file)
@@ -52,6 +52,17 @@ class Deliver {
      *                               message inside another (OPTIONAL; caller
      *                               should ONLY specify a value for this 
      *                               when the message being sent is a reply)
+     * @param resource $imap_stream  If there is an open IMAP stream in
+     *                               the caller's context, it should be
+     *                               passed in here.  This is OPTIONAL,
+     *                               as one will be created if not given,
+     *                               but as some IMAP servers may baulk
+     *                               at opening more than one connection
+     *                               at a time, the caller should always
+     *                               abide if possible.  Currently, this
+     *                               stream is only used when $reply_id
+     *                               is also non-zero, but that is subject
+     *                               to change.
      * @param mixed    $extra        Any implementation-specific variables
      *                               can be passed in here and used in
      *                               an overloaded version of this method
@@ -62,7 +73,7 @@ class Deliver {
      *
      */
     function mail(&$message, $stream=false, $reply_id=0, $reply_ent_id=0,
-                  $extra=NULL) {
+                  $imap_stream=NULL, $extra=NULL) {
 
         $rfc822_header = &$message->rfc822_header;
 
@@ -81,12 +92,27 @@ class Deliver {
             global $imapConnection, $username, $imapServerAddress, 
                    $imapPort, $mailbox;
 
-            if (!is_resource($imapConnection))
-                $imapConnection = sqimap_login($username, FALSE,
+            // try our best to use an existing IMAP handle
+            //
+            $close_imap_stream = FALSE;
+            if (is_resource($imap_stream)) {
+                $my_imap_stream = $imap_stream;
+
+            } else if (is_resource($imapConnection)) {
+                $my_imap_stream = $imapConnection;
+
+            } else {
+                $close_imap_stream = TRUE;
+                $my_imap_stream = sqimap_login($username, FALSE,
                                                $imapServerAddress, $imapPort, 0);
+            }
+
+            sqimap_mailbox_select($my_imap_stream, $mailbox);
+            $reply_message = sqimap_get_message($my_imap_stream, $reply_id, $mailbox);
 
-            sqimap_mailbox_select($imapConnection, $mailbox);
-            $reply_message = sqimap_get_message($imapConnection, $reply_id, $mailbox);
+            if ($close_imap_stream) {
+                sqimap_logout($my_imap_stream);
+            }
 
             if ($reply_ent_id) {
                 /* redefine the messsage in case of message/rfc822 */