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
This commit is contained in:
Miguel Guerreiro 2015-01-27 10:04:47 +00:00
parent ba247b861d
commit 886a1fe82e

View File

@ -480,18 +480,22 @@ class Message
if (!empty($parameters['charset']) && $parameters['charset'] !== self::$charset) { if (!empty($parameters['charset']) && $parameters['charset'] !== self::$charset) {
$mb_converted = false; $mb_converted = false;
if (function_exists('mb_convert_encoding')) { if (function_exists('mb_convert_encoding')) {
try { if (!in_array($parameters['charset'], mb_list_encodings())) {
$messageBody = mb_convert_encoding($messageBody, self::$charset, $parameters['charset']); if ($structure->encoding === 0) {
$mb_converted = true; $parameters['charset'] = 'US-ASCII';
} catch (Exception $e) { } else {
// @TODO Handle exception $parameters['charset'] = 'UTF-8';
} }
} }
$messageBody = @mb_convert_encoding($messageBody, self::$charset, $parameters['charset']);
$mb_converted = true;
}
if (!$mb_converted) { if (!$mb_converted) {
try { $messageBodyConv = @iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody);
$messageBody = iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody);
} catch (Exception $e) { if ($messageBodyConv !== false) {
// @TODO Handle exception $messageBody = $messageBodyConv;
} }
} }
} }