首页 > 解决方案 > 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.

标签: phpcharacter-encodingimapwindows-1252

解决方案


在你的情况下,这一行:

$data = quoted_printable_decode($data);

需要像这样调整:

$data = mb_convert_encoding(quoted_printable_decode($data), 'UTF-8', 'Windows-1252');

更一般地说,为了应对非 UTF-8 编码,您可能需要提取charset正文部分的 :


推荐阅读