diff --git a/src/Connection.php b/src/Connection.php index 538080e9..7260f6f2 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -10,6 +10,7 @@ use Ddeboer\Imap\Exception\ImapNumMsgException; use Ddeboer\Imap\Exception\ImapQuotaException; use Ddeboer\Imap\Exception\MailboxDoesNotExistException; +use Ddeboer\Imap\Exception\SubscribeMailboxException; /** * A connection to an IMAP server that is authenticated for a user. @@ -177,4 +178,11 @@ private function initMailboxNames(): void $this->mailboxNames[$name] = $mailboxInfo; } } + + public function subscribeMailbox(string $name): void + { + if (false === \imap_subscribe($this->resource->getStream(), $this->server . \mb_convert_encoding($name, 'UTF7-IMAP', 'UTF-8'))) { + throw new SubscribeMailboxException(\sprintf('Can not subscribe to "%s" mailbox at "%s"', $name, $this->server)); + } + } } diff --git a/src/ConnectionInterface.php b/src/ConnectionInterface.php index 5d27854c..a2cf11a9 100644 --- a/src/ConnectionInterface.php +++ b/src/ConnectionInterface.php @@ -7,6 +7,7 @@ use Ddeboer\Imap\Exception\CreateMailboxException; use Ddeboer\Imap\Exception\DeleteMailboxException; use Ddeboer\Imap\Exception\MailboxDoesNotExistException; +use Ddeboer\Imap\Exception\SubscribeMailboxException; /** * A connection to an IMAP server that is authenticated for a user. @@ -76,4 +77,11 @@ public function createMailbox(string $name): MailboxInterface; * @throws DeleteMailboxException */ public function deleteMailbox(MailboxInterface $mailbox): void; + + /** + * Subscribe to mailbox. + * + * @throws SubscribeMailboxException + */ + public function subscribeMailbox(string $name): void; } diff --git a/src/Exception/SubscribeMailboxException.php b/src/Exception/SubscribeMailboxException.php new file mode 100644 index 00000000..9cb86475 --- /dev/null +++ b/src/Exception/SubscribeMailboxException.php @@ -0,0 +1,9 @@ +close(); } + + public function testSubscribeMailbox(): void + { + $connection = $this->getConnection(); + + $name = \uniqid('test_'); + $mailbox = $connection->createMailbox($name); + $connection->subscribeMailbox($name); + + $connection->deleteMailbox($mailbox); + + $this->expectException(SubscribeMailboxException::class); + $connection->subscribeMailbox($name); + } }