Skip to content

Commit

Permalink
Attachment handling fixed (Plain text files are no longer ignored)
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Apr 15, 2017
1 parent b7f6f8f commit f407047
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ foreach($aMailboxes as $oMailbox){
/** @var \Webklex\IMAP\Message $oMessage */
foreach($oMailbox->getMessages() as $oMessage){
echo $oMessage->subject.'<br />';
echo 'Attachments: '.$oMessage->getAttachments()->count().'<br />';
echo $oMessage->getHTMLBody(true);

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

### \Webklex\IMAP\Message
| Method | Arguments | Return | Description |
| ------------ | ----------------------------- | :-----: | -------------------------------------- |
| delete | | | Delete the current Message |
| restore | | | Restore a deleted Message |
| copy | string $mailbox, int $options | | Copy the current Messages to a mailbox |
| move | string $mailbox, int $options | | Move the current Messages to a mailbox |
| moveToFolder | string $mailbox | | Move the Message into an other Folder |
| hasTextBody | | | Check if the Message has a text body |
| hasHTMLBody | | | Check if the Message has a html body |
| getTextBody | | string | Get the Message text body |
| getHTMLBody | | string | Get the Message html body |
| Method | Arguments | Return | Description |
| -------------- | ----------------------------- | :---------: | -------------------------------------- |
| delete | | | Delete the current Message |
| restore | | | Restore a deleted Message |
| copy | string $mailbox, int $options | | Copy the current Messages to a mailbox |
| move | string $mailbox, int $options | | Move the current Messages to a mailbox |
| moveToFolder | string $mailbox | | Move the Message into an other Folder |
| hasTextBody | | | Check if the Message has a text body |
| hasHTMLBody | | | Check if the Message has a html body |
| getTextBody | | string | Get the Message text body |
| getHTMLBody | | string | Get the Message html body |
| getAttachments | | collection | Get all message attachments |

### \Webklex\IMAP\Folder
| Method | Arguments | Return | Description |
Expand Down
6 changes: 4 additions & 2 deletions src/IMAP/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace Webklex\IMAP;

use Guzzle\Common\Collection;

class Folder {
/**
* Client instance
Expand Down Expand Up @@ -139,10 +141,10 @@ public function setChildren($children = []) {
*
* @param string $criteria
*
* @return array
* @return \Illuminate\Support\Collection
*/
public function getMessages($criteria = 'ALL') {
return $this->client->getMessages($this, $criteria);
return collect($this->client->getMessages($this, $criteria));
}

/**
Expand Down
25 changes: 19 additions & 6 deletions src/IMAP/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private function parseBody() {
* @param mixed $partNumber
*/
private function fetchStructure($structure, $partNumber = null) {
if ($structure->type == self::TYPE_TEXT) {
if ($structure->type == self::TYPE_TEXT && $partNumber == null) {

This comment has been minimized.

Copy link
@MASNathan

MASNathan Apr 18, 2017

Hello,

Why did you add the $partNumber validation?

This comment has been minimized.

Copy link
@Webklex

Webklex Apr 18, 2017

Author Owner

Hi MASNathan,
i had to add it in order to receive text files attached to an email. Otherwise they would become a part of the email body.

if ($structure->subtype == "PLAIN") {
if (!$partNumber) {
$partNumber = 1;
Expand Down Expand Up @@ -347,7 +347,7 @@ private function fetchStructure($structure, $partNumber = null) {
if ($partNumber) {
$prefix = $partNumber . ".";
}

var_dump($structure);
$this->fetchStructure($subStruct, $prefix . ($index + 1));
}
} else {
Expand Down Expand Up @@ -410,10 +410,14 @@ private function fetchStructure($structure, $partNumber = null) {
$attachment->img_src = 'data:'.$attachment->content_type.';base64,'.base64_encode($attachment->content);
}

if ($attachment->id) {
$this->attachments[$attachment->id] = $attachment;
} else {
$this->attachments[] = $attachment;
if(property_exists($attachment, 'name')){
if($attachment->name != false){
if ($attachment->id) {
$this->attachments[$attachment->id] = $attachment;
} else {
$this->attachments[] = $attachment;
}
}
}
}
}
Expand Down Expand Up @@ -515,4 +519,13 @@ public function delete(){
public function restore(){
return imap_undelete($this->client->connection, $this->message_no);
}

/**
* Get all message attachments.
*
* @return \Illuminate\Support\Collection
*/
public function getAttachments(){
return collect($this->attachments);
}
}

0 comments on commit f407047

Please sign in to comment.