7

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

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

我还分叉并完全重写了一个用于 PHP、Imap的类,并且该类可以很好地处理电子邮件——我有一些有用的方法来检测自动回复器(用于不在办公室、旧地址等),解码 base64 和 8bit消息等

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

似乎不同的电子邮件客户端/服务解释7BIT为不同的事物。我收到了一些据称7BIT实际上Base64 编码的电子邮件。我得到了一些实际上是引用可打印编码的。还有一些没有以任何方式编码。还有一些是 HTML,但未指示为 HTML,它们也被列为7BIT...

以下是使用 7Bit 编码接收的消息正文的一些示例(片段):

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_*),但在我可以将它们作为纯文本传递之前,它们显然需要更多的解码。有什么方法可以可靠地将所有具有所谓 7Bit 编码的消息转换为纯文本?

4

3 回答 3

10

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

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

/**
 * 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;
}

这取自我在 GitHub 上的 PHP 的 Imap 类的1.0-beta2 版本。

如果您对提高效率有任何想法,请告诉我。我最初尝试通过quoted_printable_decode().

于 2012-10-03T00:48:39.227 回答
5

我知道这是一个老问题......但我现在遇到了这个问题,似乎 PHP 现在有一个解决方案。

这个函数imap_fetchstructure()会给你编码的类型。

0   7BIT
1   8BIT
2   BINARY
3   BASE64
4   QUOTED-PRINTABLE
5   OTHER

从那里你应该能够创建一个这样的函数来解码消息

function _encodeMessage($msg, $type){

            if($type == 0){
                return mb_convert_encoding($msg, "UTF-8", "auto");
            } elseif($type == 1){
                return imap_8bit($msg); //imap_utf8
            } elseif($type == 2){
                return imap_base64(imap_binary($msg));
            } elseif($type == 3){
                return imap_base64($msg);
            } elseif($type == 4){
                return imap_qprint($msg);
                //return quoted_printable_decode($msg);
            } else {
                return $msg;
            }
        }

你可以像这样调用这个函数

$struct = imap_fetchstructure($conn, $messageNumber, 0);
$message = imap_fetchbody($conn, $messageNumber, 1);
$message = _encodeMessage($message, $struct->encoding);
echo $message;

我希望这可以帮助别人 :)

于 2015-03-16T15:58:15.517 回答
0

$structure = imap_fetchstructure; 不是$encoding = $structure->encoding$encoding = $structure->parts[ $p ]->encoding

我想我遇到了同样的问题,现在已经解决了。(7bit 没有转换为 UTF-8,一直得到 ASCII)我以为我有 7bit,但是将代码更改为“但”我得到了$encoding=4,这并不$encoding=0意味着我必须得到我想要的东西imap_qprint($body)mb_convert_encoding($body, 'UTF-8', $charset)

无论如何检查编码号!(应该是 4 不为零)

于 2017-06-14T20:57:20.280 回答