Skip to content

Commit a0c1558

Browse files
sparavaloWebklex
authored andcommitted
Added option for optional attachment download (#76)
* Added option for optional attachment download * Fixed majority that scrutinizer-ci found * fixed typo
1 parent 803c46c commit a0c1558

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

src/IMAP/Folder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public function searchMessages(array $where, $fetch_options = null, $parse_body
284284
if ($availableMessages !== false) {
285285
$msglist = 1;
286286
foreach ($availableMessages as $msgno) {
287-
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $parse_body);
287+
$fetch_attachments = config('imap.options.attachments', false);
288+
$message = new Message($msgno, $msglist, $this->getClient(), $fetch_options, $parse_body, $fetch_attachments);
288289

289290
$messages->put($message->getMessageId(), $message);
290291
$msglist++;

src/IMAP/Message.php

Lines changed: 32 additions & 4 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 attachments options
48+
*
49+
* @var bool
50+
*/
51+
public $fetch_attachment = null;
52+
4653
/**
4754
* @var int $msglist
4855
*/
@@ -128,9 +135,11 @@ class Message {
128135
* @param Client $client
129136
* @param integer|null $fetch_options
130137
* @param boolean $parse_body
138+
* @param boolean $fetch_attachment
131139
*/
132-
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $parse_body = false) {
140+
public function __construct($uid, $msglist, Client $client, $fetch_options = null, $parse_body = false, $fetch_attachment = false) {
133141
$this->setFetchOption($fetch_options);
142+
$this->setFetchAttachment($fetch_attachment);
134143

135144
$this->attachments = AttachmentCollection::make([]);
136145

@@ -371,8 +380,8 @@ public function parseBody() {
371380
* @param mixed $partNumber
372381
*/
373382
private function fetchStructure($structure, $partNumber = null) {
374-
if ($structure->type == self::TYPE_TEXT &&
375-
($structure->ifdisposition == 0 ||
383+
if ($structure->type == self::TYPE_TEXT &&
384+
($structure->ifdisposition == 0 ||
376385
($structure->ifdisposition == 1 && !isset($structure->parts) && $partNumber == null)
377386
)
378387
) {
@@ -421,7 +430,9 @@ private function fetchStructure($structure, $partNumber = null) {
421430
$this->fetchStructure($subStruct, $prefix.($index + 1));
422431
}
423432
} else {
424-
$this->fetchAttachment($structure, $partNumber);
433+
if ($this->fetch_attachment === true) {
434+
$this->fetchAttachment($structure, $partNumber);
435+
}
425436
}
426437
}
427438

@@ -461,6 +472,23 @@ public function setFetchOption($option) {
461472

462473
return $this;
463474
}
475+
/**
476+
* Fail proof setter for $fetch_attachment
477+
*
478+
* @param $option
479+
*
480+
* @return $this
481+
*/
482+
public function setFetchAttachment($option) {
483+
if (is_bool($option) == true) {
484+
$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;
488+
}
489+
490+
return $this;
491+
}
464492

465493
/**
466494
* Decode a given string

src/config/imap.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +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+
|
79+
| -Attachment download option
80+
| Default FALSE
81+
|
7882
| -Open IMAP options:
7983
| DISABLE_AUTHENTICATOR - Disable authentication properties.
8084
| Use 'GSSAPI' if you encounter the following
@@ -86,6 +90,7 @@
8690
'options' => [
8791
'delimiter' => '/',
8892
'fetch' => FT_UID,
93+
'attachments' => false,
8994
'open' => [
9095
// 'DISABLE_AUTHENTICATOR' => 'GSSAPI'
9196
]

0 commit comments

Comments
 (0)