我的PHP应用程序处理收到的电子邮件。处理代码通常运行良好,但该应用程序最近崩溃,只有以下例外:
Unexpected encoding - UTF-8 or ASCII was expected (View: /home/customer/www/gonativeguide.com/gng2-core/vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/panel.blade.php) {"exception":"[object] (Facade\\Ignition\\Exceptions\\ViewException(code: 0): Unexpected encoding - UTF-8 or ASCII was expected (View: /home/customer/www/gonativeguide.com/gng2-core/vendor/laravel/framework/src/Illuminate/Mail/resources/views/html/panel.blade.php) at /home/customer/www/gonativeguide.com/gng2-core/vendor/league/commonmark/src/Input/MarkdownInput.php:30)
似乎有一封收到的电子邮件的文本没有被正确地解码,这使得应用程序后来崩溃。
我意识到这封电子邮件的编码是Windows-1252:
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
电子邮件解码代码当前如下所示:
// DECODE DATA
$data = ($partno)?
imap_fetchbody($mbox,$mid,$partno): // multipart
imap_body($mbox,$mid); // simple
// Any part may be encoded, even plain text messages, so check everything.
if ($p->encoding==4)
$data = quoted_printable_decode($data);
elseif ($p->encoding==3)
$data = base64_decode($data);
我检查了这页面,以了解我需要用Windows1252修改什么来解码电子邮件,但我不清楚哪个值对应于Windows1252,以及如何将数据解码并转换为UTF-8。我会非常感谢任何提示,最好与建议的代码在此。
谢谢你,W.
发布于 2022-01-05 20:54:47
在你的例子中,这一行:
$data = quoted_printable_decode($data);
需要作如下调整:
$data = mb_convert_encoding(quoted_printable_decode($data), 'UTF-8', 'Windows-1252');
更一般地,为了处理非UTF-8编码,您可能需要提取身体部分的charset
:
imap_bodystruct()
返回的身体部分结构,或imap_fetchmime()
返回的主体部分MIME标头。https://stackoverflow.com/questions/68268293
复制相似问题