php - PHP imap: how to decode and convert Windows-1252 charset emails?
问题描述
My PHP app processes incoming emails. The processing code usually works fine, but the app crashed recently with the below exception:
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)
It seems that there was an incoming email whose text was not properly decoded and this made the app crash later on.
I realized that the email had a Windows-1252 encoding:
Content-Type: text/html; charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
The email decoding code looks currently like this:
// 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);
I checked this page to understand what I need to change to decode emails with Windows-1252, but it not clear to me which value corresponds to Windows-1252 and how to decode and convert the data to UTF-8. I would highly appreciate any hints, preferably with suggested code on this.
Thanks, W.
解决方案
在你的情况下,这一行:
$data = quoted_printable_decode($data);
需要像这样调整:
$data = mb_convert_encoding(quoted_printable_decode($data), 'UTF-8', 'Windows-1252');
更一般地说,为了应对非 UTF-8 编码,您可能需要提取charset
正文部分的 :
- 从正文部分结构,由返回
imap_bodystruct()
,或 - 来自正文部分 MIME 标头,由
imap_fetchmime()
.
推荐阅读
- python - 尝试使用循环从网站导入数据时出错
- javascript - 如何更好地格式化从 api 提取到 ant 设计表的数据
- javascript - 全局对象在自定义模块中不可用,即使在传入和请求时也是如此
- c - 在 C 语言中超越风格?
- c# - Android 服务 - 应用程序关闭后无法停止
- jquery - select2 在刷新时返回 null
- c - 如何将单词对从文件存储到字符串?
- python - Crontab 停止 - Ubuntu 服务器的管道损坏
- vb.net - 在图形用户界面中更改“灯”颜色指示器 (Visual Studio 2019)
- visual-studio-code - 没有扩展名的文件的 Visual Studio Code 多行注释