diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index e77984a..e6b0478 100644 --- a/src/Fetch/Attachment.php +++ b/src/Fetch/Attachment.php @@ -97,8 +97,10 @@ public function __construct(Message $message, $structure, $partIdentifier = null } elseif (isset($parameters['name'])) { $this->setFileName($parameters['name']); } - - $this->size = $structure->bytes; + + if (isset($structure->bytes)) { + $this->size = $structure->bytes; + } $this->mimeType = Message::typeIdToString($structure->type); @@ -135,7 +137,7 @@ public function getData() */ public function getFileName() { - return (isset($this->filename)) ? $this->filename : false; + return (isset($this->filename)) ? preg_replace('/[^A-Za-z0-9\-_.]/', '_', $this->filename) : false; } /** diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index b63d72b..3fc5535 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -36,8 +36,15 @@ public static function decode($text, $targetCharset = 'utf-8') foreach (imap_mime_header_decode($text) as $word) { $ch = 'default' === $word->charset ? 'ascii' : $word->charset; + $text = $word->text; + // Use the possible false return of `mb_encoding_aliases()` to detect whether we can process the encoding + if (function_exists('mb_convert_encoding') && @mb_encoding_aliases($ch)) { + // This will strip any unrecognised characters and ensure we avoid + // "Detected an incomplete multibyte character in input string" errors + $text = mb_convert_encoding($text, $ch, $ch); + } - $result .= iconv($ch, $targetCharset, $word->text); + $result .= iconv($ch, $targetCharset, $text); } return $result; diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..6d0fa7a 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -191,6 +191,14 @@ class Message */ public static $charsetFlag = '//TRANSLIT'; + /** + * This value defines the flag set for encoding for iconv to ignore the + * iconv(): Detected an illegal character in input string. + * + * @var string + */ + public static $charsetAltFlag = '//IGNORE'; + /** * These constants can be used to easily access available flags */ @@ -230,12 +238,15 @@ protected function loadMessage() /* First load the message overview information */ if(!is_object($messageOverview = $this->getOverview())) - return false; - $this->subject = MIME::decode($messageOverview->subject, self::$charset); - $this->date = strtotime($messageOverview->date); - $this->size = $messageOverview->size; + $subject = property_exists($messageOverview, 'subject')? $messageOverview->subject : ''; + $date = property_exists($messageOverview, 'date')? $messageOverview->date : ''; + $size = property_exists($messageOverview, 'size')? $messageOverview->size : ''; + + $this->subject = MIME::decode($subject, self::$charset . self::$charsetAltFlag); + $this->date = strtotime($date); + $this->size = $size; foreach (self::$flagTypes as $flag) $this->status[$flag] = ($messageOverview->$flag == 1); @@ -562,15 +573,21 @@ protected function processStructure($structure, $partIdentifier = null) } } - if (isset($structure->parts)) { // multipart: iterate through each part + if (! empty($structure->parts)) { - foreach ($structure->parts as $partIndex => $part) { - $partId = $partIndex + 1; + if (isset($structure->subtype) && strtolower($structure->subtype) === 'rfc822') { + // rfc822: The root part is processed with the current part identifier + $this->processStructure($structure->parts[0], $partIdentifier); + } else { + // multipart: iterate through each part + foreach ($structure->parts as $partIndex => $part) { + $partId = $partIndex + 1; - if (isset($partIdentifier)) - $partId = $partIdentifier . '.' . $partId; + if (isset($partIdentifier)) + $partId = $partIdentifier . '.' . $partId; - $this->processStructure($part, $partId); + $this->processStructure($part, $partId); + } } } } @@ -672,9 +689,10 @@ protected function processAddressObject($addresses) foreach ($addresses as $address) { if (property_exists($address, 'mailbox') && $address->mailbox != 'undisclosed-recipients') { $currentAddress = array(); - $currentAddress['address'] = $address->mailbox . '@' . $address->host; + $host = property_exists($address, 'host')?$address->host:''; + $currentAddress['address'] = $address->mailbox . '@' . $host; if (isset($address->personal)) { - $currentAddress['name'] = MIME::decode($address->personal, self::$charset); + $currentAddress['name'] = MIME::decode($address->personal, self::$charset . self::$charsetAltFlag); } $outputAddresses[] = $currentAddress; } diff --git a/src/Fetch/Server.php b/src/Fetch/Server.php index 32e57c1..a90c7e7 100644 --- a/src/Fetch/Server.php +++ b/src/Fetch/Server.php @@ -439,6 +439,16 @@ public function getMessageByUid($uid) } } + /** + * This function returns all the imap errors to prevent the following + * warning from imap_close and imap_expunge + * 'Unknown: Warning: MIME header encountered in non-MIME message (errflg=3)' + */ + public function getImapErrors() + { + return imap_errors(); + } + /** * This function removes all of the messages flagged for deletion from the mailbox. *