From 886a1fe82ebbceb16067659998ee63e17603a817 Mon Sep 17 00:00:00 2001 From: Miguel Guerreiro Date: Tue, 27 Jan 2015 10:04:47 +0000 Subject: [PATCH] Fix for unsupported encodings When using mb_convert_encoding, check identified charset against list of available encodings. If none match use US-ASCII if encoding is 7-bit; UTF-8 otherwise. When using iconv, suppress errors and check if the function returns false --- src/Fetch/Message.php | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index 67bb1bf..a135ca2 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -480,18 +480,22 @@ class Message if (!empty($parameters['charset']) && $parameters['charset'] !== self::$charset) { $mb_converted = false; if (function_exists('mb_convert_encoding')) { - try { - $messageBody = mb_convert_encoding($messageBody, self::$charset, $parameters['charset']); - $mb_converted = true; - } catch (Exception $e) { - // @TODO Handle exception + if (!in_array($parameters['charset'], mb_list_encodings())) { + if ($structure->encoding === 0) { + $parameters['charset'] = 'US-ASCII'; + } else { + $parameters['charset'] = 'UTF-8'; + } } + + $messageBody = @mb_convert_encoding($messageBody, self::$charset, $parameters['charset']); + $mb_converted = true; } if (!$mb_converted) { - try { - $messageBody = iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody); - } catch (Exception $e) { - // @TODO Handle exception + $messageBodyConv = @iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody); + + if ($messageBodyConv !== false) { + $messageBody = $messageBodyConv; } } }