Skip to content

Commit 7e53afe

Browse files
committed
Query extended with markAsRead() and leaveUnread() methods
1 parent f22d863 commit 7e53afe

File tree

4 files changed

+70
-29
lines changed

4 files changed

+70
-29
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ $oMessage->setFlag(['Seen', 'Spam']);
268268
$oMessage->unsetFlag('Spam');
269269
```
270270

271+
Mark all messages as "read" while fetching:
272+
``` php
273+
/** @var \Webklex\IMAP\Folder $oFolder */
274+
/** @var \Webklex\IMAP\Support\MessageCollection $aMessage */
275+
$aMessage = $oFolder->query()->text('Hello world')->markAsRead()->get();
276+
```
277+
278+
Don't mark all messages as "read" while fetching:
279+
``` php
280+
/** @var \Webklex\IMAP\Folder $oFolder */
281+
/** @var \Webklex\IMAP\Support\MessageCollection $aMessage */
282+
$aMessage = $oFolder->query()->text('Hello world')->leaveUnread()->get();
283+
```
284+
271285
Save message attachments:
272286
``` php
273287
/** @var \Webklex\IMAP\Message $oMessage */
@@ -433,6 +447,8 @@ $oFolder = $aMessage->getContainingFolder();
433447
| setFetchBody | boolean $fetch_body | WhereQuery | Set the fetch body option |
434448
| getFetchAttachment | boolean $fetch_attachment | WhereQuery | Set the fetch attachment option |
435449
| setFetchFlags | boolean $fetch_flags | WhereQuery | Set the fetch flags option |
450+
| leaveUnread | | WhereQuery | Don't mark all messages as "read" while fetching: |
451+
| markAsRead | | WhereQuery | Mark all messages as "read" while fetching |
436452
437453
### [Attachment::class](src/IMAP/Attachment.php)
438454
| Method | Arguments | Return | Description |

src/IMAP/Attachment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ protected function findType() {
131131
*/
132132
protected function fetch() {
133133

134-
$content = imap_fetchbody($this->oMessage->getClient()->getConnection(), $this->oMessage->getUid(), $this->part_number, $this->oMessage->getFetchOptions());
134+
$content = imap_fetchbody($this->oMessage->getClient()->getConnection(), $this->oMessage->getUid(), $this->part_number, $this->oMessage->getFetchOptions() | FT_UID);
135135

136136
$this->content_type = $this->type.'/'.strtolower($this->structure->subtype);
137137
$this->content = $this->oMessage->decodeString($content, $this->structure->encoding);

src/IMAP/Message.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class Message {
7070
*/
7171
public $msglist = 1;
7272

73+
/**
74+
* @var int $msgn
75+
*/
76+
public $msgn = null;
77+
7378
/**
7479
* @var string $header
7580
*/
@@ -169,8 +174,10 @@ public function __construct($uid, $msglist, Client $client, $fetch_options = nul
169174

170175
$this->msglist = $msglist;
171176
$this->client = $client;
172-
$this->uid = ($this->fetch_options == FT_UID) ? $uid : imap_msgno($this->client->getConnection(), $uid);
173-
177+
178+
$this->uid = ($this->fetch_options == FT_UID) ? $uid : $uid;
179+
$this->msgn = ($this->fetch_options == FT_UID) ? imap_msgno($this->client->getConnection(), $uid) : $uid;
180+
174181
$this->parseHeader();
175182

176183
if ($this->getFetchFlagsOption() === true) {
@@ -267,7 +274,7 @@ public function getHTMLBody($replaceImages = false) {
267274
* @return void
268275
*/
269276
private function parseHeader() {
270-
$this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, $this->fetch_options);
277+
$this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, FT_UID);
271278
if ($this->header) {
272279
$header = imap_rfc822_parse_headers($this->header);
273280
}
@@ -353,7 +360,7 @@ private function parseHeader() {
353360
* @return void
354361
*/
355362
private function parseFlags() {
356-
$flags = imap_fetch_overview($this->client->getConnection(), $this->uid, $this->fetch_options);
363+
$flags = imap_fetch_overview($this->client->getConnection(), $this->uid, FT_UID);
357364
if (is_array($flags) && isset($flags[0])) {
358365
if (property_exists($flags[0], 'recent')) {
359366
$this->flags->put('recent', $flags[0]->recent);
@@ -430,7 +437,7 @@ private function parseAddresses($list) {
430437
* @return $this
431438
*/
432439
public function parseBody() {
433-
$structure = imap_fetchstructure($this->client->getConnection(), $this->uid, $this->fetch_options);
440+
$structure = imap_fetchstructure($this->client->getConnection(), $this->uid, FT_UID);
434441

435442
if(property_exists($structure, 'parts')){
436443
$parts = $structure->parts;
@@ -469,7 +476,7 @@ private function fetchStructure($structure, $partNumber = null) {
469476

470477
$encoding = $this->getEncoding($structure);
471478

472-
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options);
479+
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | FT_UID);
473480
$content = $this->decodeString($content, $structure->encoding);
474481
$content = $this->convertEncoding($content, $encoding);
475482

@@ -488,7 +495,7 @@ private function fetchStructure($structure, $partNumber = null) {
488495

489496
$encoding = $this->getEncoding($structure);
490497

491-
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options);
498+
$content = imap_fetchbody($this->client->getConnection(), $this->uid, $partNumber, $this->fetch_options | FT_UID);
492499
$content = $this->decodeString($content, $structure->encoding);
493500
$content = $this->convertEncoding($content, $encoding);
494501

@@ -682,7 +689,7 @@ public function getContainingFolder(Folder $folder = null) {
682689
// Try finding the message by uid in the current folder
683690
$client = new Client;
684691
$client->openFolder($folder);
685-
$uidMatches = imap_fetch_overview($client->getConnection(), $this->uid, $this->fetch_options);
692+
$uidMatches = imap_fetch_overview($client->getConnection(), $this->uid, FT_UID);
686693
$uidMatch = count($uidMatches)
687694
? new Message($uidMatches[0]->uid, $uidMatches[0]->msgno, $client)
688695
: null;
@@ -728,7 +735,7 @@ public function moveToFolder($mailbox = 'INBOX', $options = 0) {
728735
* @return bool
729736
*/
730737
public function delete() {
731-
$status = imap_delete($this->client->getConnection(), $this->uid, $this->fetch_options);
738+
$status = imap_delete($this->client->getConnection(), $this->uid, FT_UID);
732739
$this->client->expunge();
733740

734741
return $status;
@@ -740,7 +747,7 @@ public function delete() {
740747
* @return bool
741748
*/
742749
public function restore() {
743-
return imap_undelete($this->client->getConnection(), $this->message_no);
750+
return imap_undelete($this->client->getConnection(), $this->uid, FT_UID);
744751
}
745752

746753
/**
@@ -788,7 +795,7 @@ public function unsetFlag($flag) {
788795
*/
789796
public function getRawBody() {
790797
if ($this->raw_body === null) {
791-
$this->raw_body = imap_fetchbody($this->client->getConnection(), $this->getMessageNo(), '');
798+
$this->raw_body = imap_fetchbody($this->client->getConnection(), $this->getUid(), '', $this->fetch_options | FT_UID);
792799
}
793800

794801
return $this->raw_body;

src/IMAP/Query/Query.php

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class Query {
3535
/** @var string $charset */
3636
protected $charset;
3737

38-
/** @var Client $oClient */
39-
protected $oClient;
38+
/** @var Client $client */
39+
protected $client;
4040

4141
/** @var int $limit */
4242
protected $limit = null;
@@ -58,11 +58,14 @@ class Query {
5858

5959
/**
6060
* Query constructor.
61-
* @param Client $oClient
61+
* @param Client $client
6262
* @param string $charset
6363
*/
64-
public function __construct(Client $oClient, $charset = 'UTF-8') {
65-
$this->oClient = $oClient;
64+
public function __construct(Client $client, $charset = 'UTF-8') {
65+
$this->setClient($client);
66+
67+
if(config('imap.options.fetch') === FT_PEEK) $this->leaveUnread();
68+
6669
$this->charset = $charset;
6770
$this->query = collect();
6871
$this->boot();
@@ -108,6 +111,28 @@ protected function parse_date($date) {
108111
return $date;
109112
}
110113

114+
/**
115+
* Don't mark messages as read when fetching
116+
*
117+
* @return $this
118+
*/
119+
public function leaveUnread() {
120+
$this->setFetchOptions(FT_PEEK);
121+
122+
return $this;
123+
}
124+
125+
/**
126+
* Mark all messages as read when fetching
127+
*
128+
* @return $this
129+
*/
130+
public function markAsRead() {
131+
$this->setFetchOptions(FT_UID);
132+
133+
return $this;
134+
}
135+
111136
/**
112137
* Fetch the current query and return all found messages
113138
*
@@ -195,8 +220,8 @@ public function generate_query() {
195220
* @return Client
196221
*/
197222
public function getClient() {
198-
$this->oClient->checkConnection();
199-
return $this->oClient;
223+
$this->client->checkConnection();
224+
return $this->client;
200225
}
201226

202227
/**
@@ -262,18 +287,11 @@ public function setCharset($charset) {
262287
}
263288

264289
/**
265-
* @return Client
266-
*/
267-
public function getOClient() {
268-
return $this->oClient;
269-
}
270-
271-
/**
272-
* @param Client $oClient
290+
* @param Client $client
273291
* @return Query
274292
*/
275-
public function setOClient($oClient) {
276-
$this->oClient = $oClient;
293+
public function setClient(Client $client) {
294+
$this->client = $client;
277295
return $this;
278296
}
279297

0 commit comments

Comments
 (0)