Skip to content

Commit 9d1f301

Browse files
committed
fetch_body option added
1 parent 4228b3b commit 9d1f301

File tree

5 files changed

+98
-50
lines changed

5 files changed

+98
-50
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ $aMessage = $oFolder->getMessages('ALL', null, false);
282282
| getFolders | bool $hierarchical, string or null $parent_folder | FolderCollection | Get folders list. If hierarchical order is set to true, it will make a tree of folders, otherwise it will return flat array. |
283283
| openFolder | Folder $folder, $attempts | | Open a given folder. |
284284
| createFolder | string $name | | Create a new folder. |
285-
| getMessages | Folder $folder, string $criteria, bool $parse_body | MessageCollection | Get messages from folder. |
286-
| getUnseenMessages | Folder $folder, string $criteria, bool $parse_body | MessageCollection | Get Unseen messages from folder. |
287-
| searchMessages | array $where, Folder $folder, $fetch_options, bool $parse_body, string $charset | MessageCollection | Get specific messages from a given folder. |
285+
| getMessages | Folder $folder, string $criteria, bool $fetch_body, bool $fetch_attachment | MessageCollection | Get messages from folder. |
286+
| getUnseenMessages | Folder $folder, string $criteria, bool $fetch_body, bool $fetch_attachment | MessageCollection | Get Unseen messages from folder. |
287+
| searchMessages | array $where, Folder $folder, $fetch_options, bool $fetch_body, string $charset, bool $fetch_attachment | MessageCollection | Get specific messages from a given folder. |
288288
| getQuota | | array | Retrieve the quota level settings, and usage statics per mailbox |
289289
| getQuotaRoot | string $quota_root | array | Retrieve the quota settings per user |
290290
| countMessages | | int | Gets the number of messages in the current mailbox |
@@ -336,10 +336,10 @@ $aMessage = $oFolder->getMessages('ALL', null, false);
336336
| ----------------- | ----------------------------------------------------------------------------------- | :---------------: | ---------------------------------------------- |
337337
| hasChildren | | bool | Determine if folder has children. |
338338
| setChildren | array $children | self | Set children. |
339-
| getMessage | integer $uid, integer or null $msglist, int or null fetch_options, bool $parse_body | Message | Get a specific message from folder. |
340-
| getMessages | string $criteria, bool $parse_body | MessageCollection | Get messages from folder. |
341-
| getUnseenMessages | string $criteria, bool $parse_body | MessageCollection | Get Unseen messages from folder. |
342-
| searchMessages | array $where, $fetch_options, bool $parse_body, string $charset | MessageCollection | Get specific messages from a given folder. |
339+
| getMessage | integer $uid, integer or null $msglist, int or null fetch_options, bool $fetch_body, bool $fetch_attachment | Message | Get a specific message from folder. |
340+
| getMessages | string $criteria, bool $fetch_body, bool $fetch_attachment | MessageCollection | Get messages from folder. |
341+
| getUnseenMessages | string $criteria, bool $fetch_body, bool $fetch_attachment | MessageCollection | Get Unseen messages from folder. |
342+
| searchMessages | array $where, $fetch_options, bool $fetch_body, string $charset, bool $fetch_attachment | MessageCollection | Get specific messages from a given folder. |
343343
| delete | | | Delete the current Mailbox |
344344
| move | string $mailbox | | Move or Rename the current Mailbox |
345345
| getStatus | integer $options | object | Returns status information on a mailbox |

src/IMAP/Client.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ public function createFolder($name) {
330330
* @param Folder $folder
331331
* @param string $criteria
332332
* @param integer $fetch_options
333-
* @param boolean $parse_body
333+
* @param boolean $fetch_body
334+
* @param boolean $fetch_attachment
334335
*
335336
* @return MessageCollection
336337
* @throws ConnectionFailedException
@@ -340,17 +341,18 @@ public function createFolder($name) {
340341
* @deprecated 1.0.5.2:2.0.0 No longer needed. Use Folder::getMessages() instead
341342
* @see Folder::getMessages()
342343
*/
343-
public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $parse_body = true) {
344-
return $folder->getMessages($criteria, $fetch_options, $parse_body);
344+
public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true) {
345+
return $folder->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment);
345346
}
346347

347348
/**
348349
* Get all unseen messages from folder
349350
*
350-
* @param Folder $folder
351-
* @param string $criteria
352-
* @param null $fetch_options
353-
* @param bool $parse_body
351+
* @param Folder $folder
352+
* @param string $criteria
353+
* @param null $fetch_options
354+
* @param boolean $fetch_body
355+
* @param boolean $fetch_attachment
354356
*
355357
* @return MessageCollection
356358
* @throws ConnectionFailedException
@@ -360,8 +362,8 @@ public function getMessages(Folder $folder, $criteria = 'ALL', $fetch_options =
360362
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
361363
* @see Folder::getMessages()
362364
*/
363-
public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $parse_body = true) {
364-
return $folder->getUnseenMessages($criteria, $fetch_options, $parse_body);
365+
public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true) {
366+
return $folder->getUnseenMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment);
365367
}
366368

367369
/**
@@ -370,8 +372,9 @@ public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_o
370372
* @param array $where
371373
* @param Folder $folder
372374
* @param null $fetch_options
373-
* @param boolean $parse_body
375+
* @param boolean $fetch_body
374376
* @param string $charset
377+
* @param boolean $fetch_attachment
375378
*
376379
* @return MessageCollection
377380
* @throws ConnectionFailedException
@@ -382,8 +385,8 @@ public function getUnseenMessages(Folder $folder, $criteria = 'UNSEEN', $fetch_o
382385
* @see Folder::searchMessages()
383386
*
384387
*/
385-
public function searchMessages(array $where, Folder $folder, $fetch_options = null, $parse_body = true, $charset = "UTF-8") {
386-
return $folder->searchMessages($where, $fetch_options, $parse_body, $charset);
388+
public function searchMessages(array $where, Folder $folder, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true) {
389+
return $folder->searchMessages($where, $fetch_options, $fetch_body, $charset, $fetch_attachment);
387390
}
388391

389392
/**

src/IMAP/Folder.php

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ public function setChildren($children = []) {
151151
* @param integer $uid Please note that the uid is not unique and can change
152152
* @param integer|null $msglist
153153
* @param integer|null $fetch_options
154-
* @param boolean $parse_body
154+
* @param boolean $fetch_body
155+
* @param boolean $fetch_attachment
155156
*
156157
* @return Message|null
157158
*/
158-
public function getMessage($uid, $msglist = null, $fetch_options = null, $parse_body = false) {
159+
public function getMessage($uid, $msglist = null, $fetch_options = null, $fetch_body = false, $fetch_attachment = false) {
159160
if (imap_msgno($this->getClient()->getConnection(), $uid) > 0) {
160-
return new Message($uid, $msglist, $this->getClient(), $fetch_options, $parse_body);
161+
return new Message($uid, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment);
161162
}
162163

163164
return null;
@@ -166,25 +167,27 @@ public function getMessage($uid, $msglist = null, $fetch_options = null, $parse_
166167
/**
167168
* Get all messages
168169
*
169-
* @param string $criteria
170-
* @param null $fetch_options
171-
* @param bool $parse_body
170+
* @param string $criteria
171+
* @param null $fetch_options
172+
* @param boolean $fetch_body
173+
* @param boolean $fetch_attachment
172174
*
173175
* @return MessageCollection
174176
* @throws Exceptions\ConnectionFailedException
175177
* @throws GetMessagesFailedException
176178
* @throws MessageSearchValidationException
177179
*/
178-
public function getMessages($criteria = 'ALL', $fetch_options = null, $parse_body = true) {
179-
return $this->searchMessages([[$criteria]], $fetch_options, $parse_body);
180+
public function getMessages($criteria = 'ALL', $fetch_options = null, $fetch_body = true, $fetch_attachment = true) {
181+
return $this->searchMessages([[$criteria]], $fetch_options, $fetch_body, $fetch_attachment);
180182
}
181183

182184
/**
183185
* Get all unseen messages
184186
*
185-
* @param string $criteria
186-
* @param null $fetch_options
187-
* @param bool $parse_body
187+
* @param string $criteria
188+
* @param null $fetch_options
189+
* @param boolean $fetch_body
190+
* @param boolean $fetch_attachment
188191
*
189192
* @return MessageCollection
190193
* @throws Exceptions\ConnectionFailedException
@@ -194,8 +197,8 @@ public function getMessages($criteria = 'ALL', $fetch_options = null, $parse_bod
194197
* @deprecated 1.0.5:2.0.0 No longer needed. Use Folder::getMessages('UNSEEN') instead
195198
* @see Folder::getMessages()
196199
*/
197-
public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $parse_body = true) {
198-
return $this->getMessages($criteria, $fetch_options, $parse_body);
200+
public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $fetch_body = true, $fetch_attachment = true) {
201+
return $this->getMessages($criteria, $fetch_options, $fetch_body, $fetch_attachment);
199202
}
200203

201204
/**
@@ -213,9 +216,10 @@ public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $
213216
* The following sample would search for all flagged messages:
214217
* [['FLAGGED']]
215218
* ---------------------------------------------------------------------------------------
216-
* @param null $fetch_options
217-
* @param boolean $parse_body
218-
* @param string $charset
219+
* @param null $fetch_options
220+
* @param boolean $fetch_body
221+
* @param string $charset
222+
* @param boolean $fetch_attachment
219223
*
220224
* @return MessageCollection
221225
*
@@ -258,7 +262,7 @@ public function getUnseenMessages($criteria = 'UNSEEN', $fetch_options = null, $
258262
* / ( ("+" / "-") 4DIGIT ) ; Local differential
259263
* ; hours+min. (HHMM)
260264
*/
261-
public function searchMessages(array $where, $fetch_options = null, $parse_body = true, $charset = "UTF-8") {
265+
public function searchMessages(array $where, $fetch_options = null, $fetch_body = true, $charset = "UTF-8", $fetch_attachment = true) {
262266

263267
$this->getClient()->checkConnection();
264268

@@ -284,8 +288,7 @@ public function searchMessages(array $where, $fetch_options = null, $parse_body
284288
if ($availableMessages !== false) {
285289
$msglist = 1;
286290
foreach ($availableMessages as $msgno) {
287-
$fetch_attachments = config('imap.options.attachments', false);
288-
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $parse_body, $fetch_attachments);
291+
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $fetch_body, $fetch_attachment);
289292

290293
$messages->put($message->getMessageId(), $message);
291294
$msglist++;

src/IMAP/Message.php

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class Message {
4343
*/
4444
public $fetch_options = null;
4545

46+
/**
47+
* Fetch body options
48+
*
49+
* @var bool
50+
*/
51+
public $fetch_body = null;
52+
4653
/**
4754
* Fetch attachments options
4855
*
@@ -134,12 +141,13 @@ class Message {
134141
* @param integer|null $msglist
135142
* @param Client $client
136143
* @param integer|null $fetch_options
137-
* @param boolean $parse_body
144+
* @param boolean $fetch_body
138145
* @param boolean $fetch_attachment
139146
*/
140-
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $parse_body = false, $fetch_attachment = false) {
147+
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $fetch_body = false, $fetch_attachment = false) {
141148
$this->setFetchOption($fetch_options);
142-
$this->setFetchAttachment($fetch_attachment);
149+
$this->setFetchBodyOption($fetch_body);
150+
$this->setFetchAttachmentOption($fetch_attachment);
143151

144152
$this->attachments = AttachmentCollection::make([]);
145153

@@ -149,7 +157,7 @@ public function __construct($uid, $msglist, Client $client, $fetch_options = nul
149157

150158
$this->parseHeader();
151159

152-
if ($parse_body !== false) {
160+
if ($this->getFetchBodyOption() === true) {
153161
$this->parseBody();
154162
}
155163
}
@@ -465,26 +473,45 @@ protected function fetchAttachment($structure, $partNumber) {
465473
public function setFetchOption($option) {
466474
if (is_long($option) == true) {
467475
$this->fetch_options = $option;
468-
} elseif(is_null($option) == true) {
476+
} elseif (is_null($option) == true) {
469477
$config = config('imap.options.fetch', FT_UID);
470478
$this->fetch_options = is_long($config) ? $config : 1;
471479
}
472480

473481
return $this;
474482
}
483+
484+
/**
485+
* Fail proof setter for $fetch_body
486+
*
487+
* @param $option
488+
*
489+
* @return $this
490+
*/
491+
public function setFetchBodyOption($option) {
492+
if (is_bool($option) == true) {
493+
$this->fetch_body = $option;
494+
} elseif (is_null($option) == true) {
495+
$config = config('imap.options.fetch_body', true);
496+
$this->fetch_body = is_bool($config) ? $config : true;
497+
}
498+
499+
return $this;
500+
}
501+
475502
/**
476503
* Fail proof setter for $fetch_attachment
477504
*
478505
* @param $option
479506
*
480507
* @return $this
481508
*/
482-
public function setFetchAttachment($option) {
509+
public function setFetchAttachmentOption($option) {
483510
if (is_bool($option) == true) {
484511
$this->fetch_attachment = $option;
485-
} elseif(is_null($option) == true) {
486-
$config = config('imap.options.attachments', false);
487-
$this->fetch_attachment = is_bool($config) ? $config : false;
512+
} elseif (is_null($option) == true) {
513+
$config = config('imap.options.fetch_attachment', true);
514+
$this->fetch_attachment = is_bool($config) ? $config : true;
488515
}
489516

490517
return $this;
@@ -662,6 +689,20 @@ public function getFetchOptions() {
662689
return $this->fetch_options;
663690
}
664691

692+
/**
693+
* @return boolean
694+
*/
695+
public function getFetchBodyOption() {
696+
return $this->fetch_body;
697+
}
698+
699+
/**
700+
* @return boolean
701+
*/
702+
public function getFetchAttachmentOption() {
703+
return $this->fetch_attachment;
704+
}
705+
665706
/**
666707
* @return int
667708
*/

src/config/imap.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@
7575
| -Fetch option:
7676
| FT_UID - Message marked as read by fetching the message
7777
| FT_PEEK - Fetch the message without setting the "read" flag
78-
|
78+
| -Body download option
79+
| Default TRUE
7980
| -Attachment download option
80-
| Default FALSE
81-
|
81+
| Default TRUE
8282
| -Open IMAP options:
8383
| DISABLE_AUTHENTICATOR - Disable authentication properties.
8484
| Use 'GSSAPI' if you encounter the following
@@ -90,6 +90,7 @@
9090
'options' => [
9191
'delimiter' => '/',
9292
'fetch' => FT_UID,
93+
'fetch_body' => true,
9394
'fetch_attachment' => true,
9495
'open' => [
9596
// 'DISABLE_AUTHENTICATOR' => 'GSSAPI'

0 commit comments

Comments
 (0)