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) {
$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;
}
}
}