Skip to content

Commit f407047

Browse files
committed
Attachment handling fixed (Plain text files are no longer ignored)
1 parent b7f6f8f commit f407047

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

README.md

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ foreach($aMailboxes as $oMailbox){
9595
/** @var \Webklex\IMAP\Message $oMessage */
9696
foreach($oMailbox->getMessages() as $oMessage){
9797
echo $oMessage->subject.'<br />';
98+
echo 'Attachments: '.$oMessage->getAttachments()->count().'<br />';
9899
echo $oMessage->getHTMLBody(true);
99100

100101
//Move the current Message to 'INBOX.read'
@@ -157,17 +158,18 @@ You can define your accounts inside the `config/imap.php` file:
157158
| checkCurrentMailbox | | object | Check current mailbox |
158159

159160
### \Webklex\IMAP\Message
160-
| Method | Arguments | Return | Description |
161-
| ------------ | ----------------------------- | :-----: | -------------------------------------- |
162-
| delete | | | Delete the current Message |
163-
| restore | | | Restore a deleted Message |
164-
| copy | string $mailbox, int $options | | Copy the current Messages to a mailbox |
165-
| move | string $mailbox, int $options | | Move the current Messages to a mailbox |
166-
| moveToFolder | string $mailbox | | Move the Message into an other Folder |
167-
| hasTextBody | | | Check if the Message has a text body |
168-
| hasHTMLBody | | | Check if the Message has a html body |
169-
| getTextBody | | string | Get the Message text body |
170-
| getHTMLBody | | string | Get the Message html body |
161+
| Method | Arguments | Return | Description |
162+
| -------------- | ----------------------------- | :---------: | -------------------------------------- |
163+
| delete | | | Delete the current Message |
164+
| restore | | | Restore a deleted Message |
165+
| copy | string $mailbox, int $options | | Copy the current Messages to a mailbox |
166+
| move | string $mailbox, int $options | | Move the current Messages to a mailbox |
167+
| moveToFolder | string $mailbox | | Move the Message into an other Folder |
168+
| hasTextBody | | | Check if the Message has a text body |
169+
| hasHTMLBody | | | Check if the Message has a html body |
170+
| getTextBody | | string | Get the Message text body |
171+
| getHTMLBody | | string | Get the Message html body |
172+
| getAttachments | | collection | Get all message attachments |
171173

172174
### \Webklex\IMAP\Folder
173175
| Method | Arguments | Return | Description |

src/IMAP/Folder.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace Webklex\IMAP;
1414

15+
use Guzzle\Common\Collection;
16+
1517
class Folder {
1618
/**
1719
* Client instance
@@ -139,10 +141,10 @@ public function setChildren($children = []) {
139141
*
140142
* @param string $criteria
141143
*
142-
* @return array
144+
* @return \Illuminate\Support\Collection
143145
*/
144146
public function getMessages($criteria = 'ALL') {
145-
return $this->client->getMessages($this, $criteria);
147+
return collect($this->client->getMessages($this, $criteria));
146148
}
147149

148150
/**

src/IMAP/Message.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private function parseBody() {
306306
* @param mixed $partNumber
307307
*/
308308
private function fetchStructure($structure, $partNumber = null) {
309-
if ($structure->type == self::TYPE_TEXT) {
309+
if ($structure->type == self::TYPE_TEXT && $partNumber == null) {
310310
if ($structure->subtype == "PLAIN") {
311311
if (!$partNumber) {
312312
$partNumber = 1;
@@ -347,7 +347,7 @@ private function fetchStructure($structure, $partNumber = null) {
347347
if ($partNumber) {
348348
$prefix = $partNumber . ".";
349349
}
350-
350+
var_dump($structure);
351351
$this->fetchStructure($subStruct, $prefix . ($index + 1));
352352
}
353353
} else {
@@ -410,10 +410,14 @@ private function fetchStructure($structure, $partNumber = null) {
410410
$attachment->img_src = 'data:'.$attachment->content_type.';base64,'.base64_encode($attachment->content);
411411
}
412412

413-
if ($attachment->id) {
414-
$this->attachments[$attachment->id] = $attachment;
415-
} else {
416-
$this->attachments[] = $attachment;
413+
if(property_exists($attachment, 'name')){
414+
if($attachment->name != false){
415+
if ($attachment->id) {
416+
$this->attachments[$attachment->id] = $attachment;
417+
} else {
418+
$this->attachments[] = $attachment;
419+
}
420+
}
417421
}
418422
}
419423
}
@@ -515,4 +519,13 @@ public function delete(){
515519
public function restore(){
516520
return imap_undelete($this->client->connection, $this->message_no);
517521
}
522+
523+
/**
524+
* Get all message attachments.
525+
*
526+
* @return \Illuminate\Support\Collection
527+
*/
528+
public function getAttachments(){
529+
return collect($this->attachments);
530+
}
518531
}

0 commit comments

Comments
 (0)