From bf54c89e8f394c0bbfe9ea917abfb2162ee51818 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Mon, 26 Oct 2015 11:07:37 +0800 Subject: [PATCH] MDL-51894 inboundmessage: Ensure that all mailboxes exist --- admin/tool/messageinbound/classes/manager.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/admin/tool/messageinbound/classes/manager.php b/admin/tool/messageinbound/classes/manager.php index 17c9a1af8e2d8..46ae5d97c4a01 100644 --- a/admin/tool/messageinbound/classes/manager.php +++ b/admin/tool/messageinbound/classes/manager.php @@ -102,6 +102,10 @@ protected function get_imap_client() { 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 @@ private function message_has_flag($messageid, $flag) { 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). *