diff --git a/src/Fetch/Attachment.php b/src/Fetch/Attachment.php index e77984a..4079c5a 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); diff --git a/src/Fetch/MIME.php b/src/Fetch/MIME.php index b63d72b..a5dc517 100644 --- a/src/Fetch/MIME.php +++ b/src/Fetch/MIME.php @@ -34,12 +34,14 @@ public static function decode($text, $targetCharset = 'utf-8') $result = ''; - foreach (imap_mime_header_decode($text) as $word) { - $ch = 'default' === $word->charset ? 'ascii' : $word->charset; + $encoding = mb_detect_encoding($text, mb_detect_order(), false); - $result .= iconv($ch, $targetCharset, $word->text); + if($encoding == "UTF-8") { + $text = mb_convert_encoding($text, 'UTF-8', 'UTF-8'); } + $result = iconv(mb_detect_encoding($text, mb_detect_order(), false), "UTF-8//IGNORE", $text); + return $result; } } diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..7faee6d 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); @@ -672,9 +683,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. *