gpt4 book ai didi

php - 使用 7 位内容传输编码解析电子邮件正文 - PHP

转载 作者:可可西里 更新时间:2023-11-01 13:10:23 24 4
gpt4 key购买 nike

我最近一直在实现一些基于 PHP/IMAP 的电子邮件处理功能,并且除了邮件正文解码(在某些情况下)之外,大多数功能都运行良好。

我想,到现在为止,我已经记住了一半RFC 2822 (“Internet 消息格式”文档指南),通读六个开源 CMS 的电子邮件处理代码,并阅读大量论坛帖子、博客帖子等,这些都是用 PHP 处理电子邮件的内容。

我还 fork 并完全重写了一个 PHP 类,Imap ,并且该类(class)可以很好地处理电子邮件——我在那里有一些有用的方法来检测自动回复(外出、旧地址等)、解码 base64 和 8 位消息等。

但是,我根本无法可靠地工作(或者,有时,根本无法)的一件事是当消息以 Content-Transfer-Encoding: 7bit 传入时。

似乎不同的电子邮件客户端/服务将 7BIT 解释为不同的意思。我收到了一些据称是 7BIT 的电子邮件,实际上 是 Base64 编码的。我得到了一些实际上 quoted-printable-encoded。还有一些没有以任何方式编码。还有一些是 HTML,但没有标明是 HTML,它们也被列为 7BIT...

以下是使用 7 位编码接收的消息正文的几个示例(片段):

1:

A random message=20

Sent from my iPhone

2:

PGh0bWwgeG1sbnM6dj0idXJuOnNjaGVtYXMtbWljcm9zb2Z0LWNvbTp2bWwi
IHhtbG5zOm89InVybjpzY2hlbWFzLW1pY3Jvc29mdC1jb206b2ZmaWNlOm9m

3:

tangerine apricot pepper.=0A=C2=A0=0ALet me know if you have any availabili=
ty over the next month or so. =0A=C2=A0=0AThank you,=0ANames Withheld=0A908=
-319-5916=0A=C2=A0=0A=C2=A0=0A=C2=A0=0A=0A=0A______________________________=
__=0AFrom: Names Witheld =0ATo: Names Withheld=

这些都是全部使用“7Bit”编码发送的(好吧,至少根据 PHP/imap_*),但它们显然需要更多的解码我可以将它们作为明文传递。有没有什么方法可以可靠地将所有假定为 7 位编码的消息转换为明文?

最佳答案

在花了更多时间后,我决定写一些启发式检测,正如 Max 在对我的原始问题的评论中所建议的那样。

我在 Imap.php 中构建了一个更强大的 decode7Bit() 方法,它通过一堆常见的编码字符(如 =A0)并将它们替换为它们的 UTF-8 等价物,然后如果它们看起来像是 base64 编码的,则还解码消息:

/**
* Decodes 7-Bit text.
*
* PHP seems to think that most emails are 7BIT-encoded, therefore this
* decoding method assumes that text passed through may actually be base64-
* encoded, quoted-printable encoded, or just plain text. Instead of passing
* the email directly through a particular decoding function, this method
* runs through a bunch of common encoding schemes to try to decode everything
* and simply end up with something *resembling* plain text.
*
* Results are not guaranteed, but it's pretty good at what it does.
*
* @param $text (string)
* 7-Bit text to convert.
*
* @return (string)
* Decoded text.
*/
public function decode7Bit($text) {
// If there are no spaces on the first line, assume that the body is
// actually base64-encoded, and decode it.
$lines = explode("\r\n", $text);
$first_line_words = explode(' ', $lines[0]);
if ($first_line_words[0] == $lines[0]) {
$text = base64_decode($text);
}

// Manually convert common encoded characters into their UTF-8 equivalents.
$characters = array(
'=20' => ' ', // space.
'=E2=80=99' => "'", // single quote.
'=0A' => "\r\n", // line break.
'=A0' => ' ', // non-breaking space.
'=C2=A0' => ' ', // non-breaking space.
"=\r\n" => '', // joined line.
'=E2=80=A6' => '…', // ellipsis.
'=E2=80=A2' => '•', // bullet.
);

// Loop through the encoded characters and replace any that are found.
foreach ($characters as $key => $value) {
$text = str_replace($key, $value, $text);
}

return $text;
}

这取自 Imap class for PHP 的 1.0-beta2 版我在 GitHub 上有。

如果您有任何提高效率的想法,请告诉我。我最初尝试通过 quoted_printable_decode() 运行所有内容,但有时 PHP 会抛出模糊且无用的异常,所以我放弃了这种方法。

关于php - 使用 7 位内容传输编码解析电子邮件正文 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12682208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com