使用 PHP 将电子邮件解码为 UTF-7

Decode email messages to UTF-7 with PHP

我一直在尝试将许多 IMAP 邮件正文转换为更具可读性的内容(UTF-8 或等效格式)。我似乎找不到开箱即用的功能。

这是我尝试解码的示例:

President Trump signed an executive order Thursday tar= geting North Korea=E2=80=99s trading partners, calling it a =E2=80=9Cpowerful=E2= =80=9D new tool aimed at isolating and de-nuclearizing the regime.


More on thi= s: http://www.foxnews.com= /politics/2017/09/21/trump-signs-executive-order-targeting-north-koreas-tra= ding-partners.html

(在上面的示例中,任何“=”,都应该有一个换行符)

我试过的一些东西:

iconv("UTF-8", "Windows-1252//TRANSLIT//IGNORE", $data); 
//this resulted in a server error 500

imap_mime_header_decode($data); 
//this outputs an array (just something that I tried; yes, I know that it is only good for headers)

iconv_mime_decode($test, 0, "ISO-8859-1");
//This works for a few messages (plaintext ones) but does not output anything for the example above; for others, it only outputs part of the message body

mb_convert_encoding($test, "UTF8");
//this results in another internal server error!

$data = str_replace("=92", "'", $data);
//I have also tried to manually find and replace an occurrence of a utf-7 (I guess) encoded string

无论如何,有些事情我做错了但不确定是什么。你们都是如何阅读使用 IMAP 检索的电子邮件正文的?

还有哪些我可以尝试的其他事情?人们必须一直这样做,但我似乎找不到解决方案...

谢谢, 罗格

您实际上并没有在这里处理 UTF-7 编码。您实际看到的是 quoted-printable.

php contains a function to decode this

我实际上已经有一段时间没有写 php 所以请原谅我的风格失败,这里有一个解码你的文本的例子:

<?php
$s = 'President Trump signed an executive order Thursday tar= geting North Korea=E2=80=99s trading partners, calling it a =E2=80=9Cpowerful=E2= =80=9D new tool aimed at isolating and de-nuclearizing the regime.';

// It's unclear why I have to replace out `= `, I have a feeling these
// are actually newlines and copy paste error?
echo quoted_printable_decode(str_replace('= ', '', $s));
?>

当运行它产生:

President Trump signed an executive order Thursday targeting North Korea’s trading partners, calling it a “powerful” new tool aimed at isolating and de-nuclearizing the regime.