我一直在尝试将许多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检索的电子邮件的正文?
我还可以尝试一些其他的事情吗?人们必须一直这样做,但我似乎无法找到解决方案......
谢谢你, ROG
答案 0 :(得分:1)
这里你实际上并没有处理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.