MDL-51894 inboundmessage: Ensure that all mailboxes exist
authorAndrew Nicols <andrew@nicols.co.uk>
Mon, 26 Oct 2015 03:07:37 +0000 (11:07 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Mon, 26 Oct 2015 04:17:15 +0000 (12:17 +0800)
admin/tool/messageinbound/classes/manager.php

index 17c9a1a..46ae5d9 100644 (file)
@@ -102,6 +102,10 @@ class manager {
         try {
             $this->client->login();
             mtrace("Connection established.");
+
+            // Ensure that mailboxes exist.
+            $this->ensure_mailboxes_exist();
+
             return true;
 
         } catch (\Horde_Imap_Client_Exception $e) {
@@ -741,6 +745,27 @@ class manager {
         return in_array($flag, $flags);
     }
 
+    /**
+     * Ensure that all mailboxes exist.
+     */
+    private function ensure_mailboxes_exist() {
+        $requiredmailboxes = array(
+            self::MAILBOX,
+            self::CONFIRMATIONFOLDER,
+        );
+
+        $existingmailboxes = $this->client->listMailboxes($requiredmailboxes);
+        foreach ($requiredmailboxes as $mailbox) {
+            if (isset($existingmailboxes[$mailbox])) {
+                // This mailbox was found.
+                continue;
+            }
+
+            mtrace("Unable to find the '{$mailbox}' mailbox - creating it.");
+            $this->client->createMailbox($mailbox);
+        }
+    }
+
     /**
      * Attempt to determine whether this message is a bulk message (e.g. automated reply).
      *