diff --git a/src/Core/Exception/FolderNotExistException.php b/src/Core/Exception/FolderNotExistException.php new file mode 100644 index 0000000..65e262b --- /dev/null +++ b/src/Core/Exception/FolderNotExistException.php @@ -0,0 +1,25 @@ + + */ + +namespace Clivern\Imap\Core\Exception; + +/** + * Connection Error Class + * + * @package Clivern\Imap\Core\Exception + */ +class FolderNotExistException extends \Exception +{ + + /** + * Class Constructor + * + * @param string $folder + */ + public function __construct($folder = null) + { + parent::__construct(sprintf("Mailbox folder not exist: %s", $folder)); + } +} \ No newline at end of file diff --git a/src/MailBox.php b/src/MailBox.php index b076b25..001cbf7 100644 --- a/src/MailBox.php +++ b/src/MailBox.php @@ -13,6 +13,7 @@ use Clivern\Imap\Core\Message\Header; use Clivern\Imap\Core\Message\Actions; use Clivern\Imap\Core\Message\Attachments; use Clivern\Imap\Core\Message\Body; +use Clivern\Imap\Core\Exception\FolderNotExistException; /** * MailBox Class @@ -32,6 +33,12 @@ class MailBox */ protected $connection; + /** + * @var array + */ + protected $folders = []; + + /** * Constructor * @@ -50,7 +57,12 @@ class MailBox */ public function setFolder($folder) { + if( !in_array($folder, $this->getFolders()) ){ + throw new FolderNotExistException($folder); + } + $this->folder = $folder; + return $this; } @@ -125,4 +137,24 @@ class MailBox $this->connection->survive($this->folder); imap_expunge($this->connection->getStream()); } + + /** + * Get Folders List + * + * @return array + */ + public function getFolders() + { + if( !empty($this->folders) ){ + return $this->folders; + } + + $this->folders = imap_getmailboxes($this->connection->getStream(), "{" . $this->connection->getServer() . "}", "*"); + + foreach ($this->folders as $key => $folder) { + $this->folders[$key] = str_replace("{" . $this->connection->getServer() . "}", "", $folder->name); + } + + return $this->folders; + } } \ No newline at end of file