diff --git a/README.md b/README.md index 5ffa55d..e33464d 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,18 @@ $query = QueryBuilder::create('INBOX.Sent') $emails = $client->getEmails($query); ``` +Both `getEmails()` and `getEmailIds()` can take a third optional `boolean` argument. it can be set to false to indicate change of flag for fetched emails to seen. + +```php +$query = QueryBuilder::create('INBOX.Sent') + ->youngerThan(3600) + ->flagSeen(true) + + ->getQuery(); + +$emails = $client->getEmails($query,'INBOX', false); // set to "false" fetched emails' state will be set to seen. "true" no change of state will occur. default is true +``` + ### Reading folders ```php diff --git a/src/Client.php b/src/Client.php index 6582435..1300216 100644 --- a/src/Client.php +++ b/src/Client.php @@ -25,6 +25,12 @@ class Client */ private $emailFactory; + /** + * + * @var boolean + */ + private $peek; + public function __construct(Horde_Imap_Client_Socket $hordeClient, EmailFactory $emailFactory = null) { $this->hordeClient = $hordeClient; @@ -63,12 +69,12 @@ public function getFolders() : array return array_keys($this->hordeClient->listMailboxes('*')); } - /** + /** * Finds the emails matching the query. If $query is null, then it will fetch the emails in the inbox. - * + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email[] */ - public function getEmails(Query $query = null) : array + public function getEmails(Query $query = null, bool $peek = true): array { $hordeQuery = new Horde_Imap_Client_Search_Query(); @@ -76,11 +82,11 @@ public function getEmails(Query $query = null) : array if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), - Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), + Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } - + $this->setPeek($peek); $this->setFlags($hordeQuery, $query); return $this->searchAndFetch($query->getFolder(), $hordeQuery); } @@ -89,10 +95,11 @@ public function getEmails(Query $query = null) : array * Finds the email Ids matching the query. If $query is null, then it will fetch the email Ids in the inbox. * * This method is obviously more efficient than getEmails() if you want to synchronize local mails. - * + * + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return string[] */ - public function getEmailIds(Query $query = null) : array + public function getEmailIds(Query $query = null, bool $peek = true): array { $hordeQuery = new Horde_Imap_Client_Search_Query(); @@ -100,20 +107,24 @@ public function getEmailIds(Query $query = null) : array if ($query->getYoungerThan() !== null) { $hordeQuery->intervalSearch( - $query->getYoungerThan(), - Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER + $query->getYoungerThan(), + Horde_Imap_Client_Search_Query::INTERVAL_YOUNGER ); } + $this->setPeek($peek); $this->setFlags($hordeQuery, $query); return $this->search($query->getFolder(), $hordeQuery); } /** + * @param String $folder the folder to get email from + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email|null Returns null if the email was not found. */ - public function getEmailFromId(string $id, string $folder = 'INBOX') + public function getEmailFromId(string $id, string $folder = 'INBOX', bool $peek = true) { + $this->setPeek($peek); $emails = $this->fetchEmails($folder, [$id]); return (count($emails) > 0) ? $emails[0] : null; @@ -121,10 +132,13 @@ public function getEmailFromId(string $id, string $folder = 'INBOX') /** * @param string[] $ids + * @param String $folder the folder to get emails from + * @param boolean $peek sets the peek option, "false" fetched emails' state will be set to seen "true" no change of state default is true * @return Email[] */ - public function getEmailsFromId(array $ids, string $folder = 'INBOX') : array + public function getEmailsFromId(array $ids, string $folder = 'INBOX', bool $peek = true) : array { + $this->setPeek($peek); return $this->fetchEmails($folder, $ids); } @@ -178,7 +192,7 @@ private function fetchEmails(string $folder, array $ids) : array $query = new Horde_Imap_Client_Fetch_Query(); $query->envelope(); $query->fullText([ - 'peek' => true, + 'peek' => $this->getPeek(), ]); $query->flags(); @@ -220,4 +234,17 @@ private function setFlags(Horde_Imap_Client_Search_Query $hordeQuery,Query $quer } } } + /** + * + * @param boolean $peek + */ + public function setPeek($peek) + { + $this->peek = $peek; + } + + public function getPeek() + { + return $this->peek; + } } diff --git a/src/Query/Query.php b/src/Query/Query.php index 6affd64..a149382 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -19,8 +19,16 @@ class Query * @var int */ private $youngerThan; + private $flags = []; - public function getFolder() : string + const FLAG_ANSWERED = 'ANSWERED'; + const FLAG_DELETED = 'DELETED'; + const FLAG_DRAFT = 'DRAFT'; + const FLAG_FLAGED = 'FLAGGED'; + const FLAG_RECENT = 'RECENT'; + const FLAG_SEEN = 'SEEN'; + + public function getFolder(): string { return $this->folder; } @@ -46,11 +54,13 @@ public function setYoungerThan(int $youngerThan) $this->youngerThan = $youngerThan; } - public function setFlags($key,$value){ + public function setFlags($key,$value) + { $this->flags[$key] = $value; } - public function getFlags(){ + public function getFlags() + { return $this->flags; } diff --git a/src/Query/QueryBuilder.php b/src/Query/QueryBuilder.php index 0443c51..253f451 100644 --- a/src/Query/QueryBuilder.php +++ b/src/Query/QueryBuilder.php @@ -43,32 +43,38 @@ public function youngerThan(int $interval) : self return $this; } - public function flagAnswered($value) { + public function flagAnswered($value) + { $this->query->setFlags(Query::FLAG_ANSWERED, $value); return $this; } - public function flagDeleted($value) { + public function flagDeleted($value) + { $this->query->setFlags(Query::FLAG_DELETED, $value); return $this; } - public function flagDraft($value) { + public function flagDraft($value) + { $this->query->setFlags(Query::FLAG_DRAFT, $value); return $this; } - public function flagFlaged($value) { + public function flagFlaged($value) + { $this->query->setFlags(Query::FLAG_FLAGED, $value); return $this; } - public function flagRecent($value) { + public function flagRecent($value) + { $this->query->setFlags(Query::FLAG_RECENT, $value); return $this; } - public function flagSeen($value) { + public function flagSeen($value) + { $this->query->setFlags(Query::FLAG_SEEN, $value); return $this; }