From 80e769d01b9d0c6130962e3a1e00352f622907e0 Mon Sep 17 00:00:00 2001 From: Dale Allan Date: Tue, 10 May 2016 11:46:43 +0200 Subject: [PATCH 1/6] Add function to fetch and clear imap errors --- src/Fetch/Server.php | 10 ++++++++++ 1 file changed, 10 insertions(+) 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. * From a0e732932ec807e60500f4b2ce52d98fe43f6aa3 Mon Sep 17 00:00:00 2001 From: Dale Allan Date: Mon, 16 May 2016 09:54:23 +0200 Subject: [PATCH 2/6] pass //IGNORE flag to prevent utf-8 decode error throwing an exception --- src/Fetch/Message.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..26e4510 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 */ @@ -233,7 +241,7 @@ protected function loadMessage() return false; - $this->subject = MIME::decode($messageOverview->subject, self::$charset); + $this->subject = MIME::decode($messageOverview->subject, self::$charset . self::$charsetAltFlag); $this->date = strtotime($messageOverview->date); $this->size = $messageOverview->size; @@ -674,7 +682,7 @@ protected function processAddressObject($addresses) $currentAddress = array(); $currentAddress['address'] = $address->mailbox . '@' . $address->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; } From 7659863d5895a6f0afc1b3d590d3665b77d3822b Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Tue, 24 May 2016 11:45:52 +0200 Subject: [PATCH 3/6] Add check for Bytes in attachments before assigning. As per unmerged pull request https://github.com/tedious/Fetch/pull/156 --- src/Fetch/Attachment.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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); From 8d0813c8994ba772943e7259a03b8cbac10ba039 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Thu, 2 Jun 2016 07:51:18 +0200 Subject: [PATCH 4/6] Add checks for subject, date & size on the message object before attempting to use them. --- src/Fetch/Message.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 26e4510..384b08b 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -238,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 . self::$charsetAltFlag); - $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); From 7dd1cff8d56f7ad5232365fda63a2e55ae6f45d1 Mon Sep 17 00:00:00 2001 From: Marnu Lombard Date: Thu, 2 Jun 2016 08:01:58 +0200 Subject: [PATCH 5/6] Check for $address->host before assigning it --- src/Fetch/Message.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 384b08b..7faee6d 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -683,7 +683,8 @@ 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 . self::$charsetAltFlag); } From d1f2a07a0c35f99adf67d8d7cb8cccc0d5324649 Mon Sep 17 00:00:00 2001 From: Dale Allan Date: Mon, 25 Mar 2019 11:14:01 +0200 Subject: [PATCH 6/6] handle utf-8 convertion better --- src/Fetch/MIME.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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; } }