4

我有这个代码。它来自Zend Reading Mail示例。

$message = $mail->getMessage(1);

// output first text/plain part
$foundPart = null;
foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
    try {
        if (strtok($part->contentType, ';') == 'text/plain') {
            $foundPart = $part;
            break;
        }
    } catch (Zend_Mail_Exception $e) {
        // ignore
    }
}
if (!$foundPart) {
    echo 'no plain text part found';
} else {
    echo $foundPart->getContent();
}

我能得到的是消息,效果很好。但是试图将消息解码为可读的东西是行不通的。我尝试过 Zend_Mime、imap_mime 和 iconv,但没有成功。

这是我得到的一个例子$foundPart->getContent();

霍尔=F3 海姆=FAr

它应该说“Halló heimúr”

我想要的只是一些图书馆,我可以在实践中“按下按钮,接收培根”。我的意思是,我只想将库指向一个 POP3 电子邮件箱,并以可读形式(没有任何编码问题)和附件获取电子邮件。

imap_mime_header_decode()给我一个具有相同数据的数组。
iconv_ mime_ decode()做同样的事情

有谁知道为什么会发生这种情况,或者我可以将它抽象出来的某个库(PHP/Python 或 Perl)

4

2 回答 2

14

我在学习如何使用 Zend_Mail 阅读电子邮件时遇到了一些类似的问题。您将需要添加 Zend_Mail 未实现的附加逻辑,例如解码编码的电子邮件和转换字符集。这是我在找到纯文本部分后正在做的事情:

$content = $foundPart->getContent();

switch ($foundPart->contentTransferEncoding) {
    case 'base64':
        $content = base64_decode($content);
        break;
    case 'quoted-printable':
        $content = quoted_printable_decode($content);
        break;
}

//find the charset
preg_match('/charset="(.+)"$/', $foundPart->contentType, $matches);
$charset = $matches[1];

if ($charset == 'iso-8859-1') {
    $content = utf8_encode($content); //convert to utf8
}
于 2011-07-11T22:27:40.267 回答
2

这可能是因为 base64 编码。Zend_Mail 文档说(在“编码”下):

...如果在 addAttachment() 调用中没有给出其他编码或稍后分配给 MIME 部分对象,则所有其他附件都通过 base64 编码。

尝试类似:

echo base64_decode($foundPart->getContent());

另外,请阅读: http: //framework.zend.com/manual/en/zend.mail.encoding.html

希望以某种方式有所帮助。

于 2009-05-07T12:24:02.957 回答