【问题标题】:PHP imap: how to decode and convert Windows-1252 charset emails?PHP imap:如何解码和转换 Windows-1252 字符集电子邮件?
【发布时间】:2021-07-06 09:50:49
【问题描述】:

我的 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);

我检查了this 页面以了解使用 Windows-1252 解码电子邮件需要更改的内容,但我不清楚哪个值对应于 Windows-1252 以及如何解码并将数据转换为 UTF-8。我将非常感谢任何提示,最好在此提供建议的代码。

谢谢, W.

【问题讨论】:

    标签: php character-encoding imap windows-1252


    【解决方案1】:

    在你的情况下,这一行:

    $data = quoted_printable_decode($data);
    

    需要这样改编:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2018-04-30
      • 2023-03-20
      • 1970-01-01
      • 2019-08-19
      相关资源
      最近更新 更多