Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to load only subscribed folders in Mailbox. #5

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "zendframework/zend-mail",
"name": "netandreus/zend-mail",
"description": "provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages",
"license": "BSD-3-Clause",
"keywords": [
Expand All @@ -16,7 +16,7 @@
"php": ">=5.5",
"zendframework/zend-crypt": "~2.5",
"zendframework/zend-loader": "~2.5",
"zendframework/zend-mime": "~2.5",
"particlebits/zend-mime": "~2.5",
"zendframework/zend-stdlib": "~2.5",
"zendframework/zend-validator": "~2.5"
},
Expand Down
21 changes: 16 additions & 5 deletions src/Protocol/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public function __destruct()
* @param string $host hostname or IP address of IMAP server
* @param int|null $port of IMAP server, default is 143 (993 for ssl)
* @param string|bool $ssl use 'SSL', 'TLS' or false
* @param array $ssl_context_options options, passed to stream_context_create()
* @throws Exception\RuntimeException
* @return string welcome message
*/
public function connect($host, $port = null, $ssl = false)
public function connect($host, $port = null, $ssl = false, $ssl_context_options = [])
{
$isTls = false;

Expand All @@ -87,7 +88,15 @@ public function connect($host, $port = null, $ssl = false)
}

ErrorHandler::start();
$this->socket = fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
$context = stream_context_create($ssl_context_options);
$this->socket = stream_socket_client(
$host.':'.$port,
$errno,
$errstr,
self::TIMEOUT_CONNECTION,
STREAM_CLIENT_CONNECT,
$context
);
$error = ErrorHandler::stop();
if (!$this->socket) {
throw new Exception\RuntimeException(sprintf(
Expand Down Expand Up @@ -606,19 +615,21 @@ public function fetch($items, $from, $to = null)
*
* @param string $reference mailbox reference for list
* @param string $mailbox mailbox name match with wildcards
* @param bool $subscribedOnly get only subscribed folders or all folders*
* @return array mailboxes that matched $mailbox as array(globalName => array('delim' => .., 'flags' => ..))
* @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
*/
public function listMailbox($reference = '', $mailbox = '*')
public function listMailbox($reference = '', $mailbox = '*', $subscribedOnly = false)
{
$command = $subscribedOnly ? 'LSUB' : 'LIST';
$result = [];
$list = $this->requestAndResponse('LIST', $this->escapeString($reference, $mailbox));
$list = $this->requestAndResponse($command, $this->escapeString($reference, $mailbox));
if (!$list || $list === true) {
return $result;
}

foreach ($list as $item) {
if (count($item) != 4 || $item[0] != 'LIST') {
if (count($item) != 4 || $item[0] != $command) {
continue;
}
$result[$item[3]] = ['delim' => $item[2], 'flags' => $item[1]];
Expand Down
9 changes: 6 additions & 3 deletions src/Storage/Imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public function getRawContent($id, $part = null)
* - password password for user 'username' [optional, default = '']
* - port port for IMAP server [optional, default = 110]
* - ssl 'SSL' or 'TLS' for secure sockets
* - ssl_context_options Options passed to stream_context_create()
* - folder select this folder [optional, default = 'INBOX']
*
* @param array $params mail reader specific parameters
Expand Down Expand Up @@ -200,9 +201,10 @@ public function __construct($params)
$password = isset($params->password) ? $params->password : '';
$port = isset($params->port) ? $params->port : null;
$ssl = isset($params->ssl) ? $params->ssl : false;
$ssl_context_options = isset($params->ssl_context_options) ? $params->ssl_context_options: [];

$this->protocol = new Protocol\Imap();
$this->protocol->connect($host, $port, $ssl);
$this->protocol->connect($host, $port, $ssl, $ssl_context_options);
if (!$this->protocol->login($params->user, $password)) {
throw new Exception\RuntimeException('cannot login, user or password wrong');
}
Expand Down Expand Up @@ -295,14 +297,15 @@ public function getNumberByUniqueId($id)
* get root folder or given folder
*
* @param string $rootFolder get folder structure for given folder, else root
* @param bool $subscribedOnly get only subscribed folders or all folders
* @throws Exception\RuntimeException
* @throws Exception\InvalidArgumentException
* @throws \Zend\Mail\Protocol\Exception\RuntimeException
* @return \Zend\Mail\Storage\Folder root or wanted folder
*/
public function getFolders($rootFolder = null)
public function getFolders($rootFolder = null, $subscribedOnly = false)
{
$folders = $this->protocol->listMailbox((string) $rootFolder);
$folders = $this->protocol->listMailbox((string) $rootFolder, '*', $subscribedOnly);
if (!$folders) {
throw new Exception\InvalidArgumentException('folder not found');
}
Expand Down