为什么此邮件消息无法正确解码?
-
08-07-2019 - |
题
我有这个代码。它来自 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();
Hall = F3 heim = FAr
应该说“Halló海姆ú R"
我想要的只是一些图书馆,我可以“按下按钮,接收培根”。在实践中。我的意思是,我只想将图书馆指向一个POP3电子邮箱,并以可读的形式(没有任何编码问题)和附件获取电子邮件。
imap_mime_header_decode()
给我一个包含相同数据的数组。
iconv_ mime_ decode()
是否相同
有没有人知道为什么会发生这种情况或某些我可以抽象出来的库(PHP / Python或Perl)
解决方案
这可能是因为base64编码。 Zend_Mail文档说(在'encoding'下):
...所有其他附件都已编码 如果没有其他编码,则通过base64 在addAttachment()调用中给出或 分配给MIME部分对象 后面。
尝试类似:
echo base64_decode($foundPart->getContent());
另外,阅读: http://framework.zend.com/manual/en/zend。 mail.encoding.html
希望以某种方式帮助。
其他提示
我在学习如何使用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
}
不隶属于 StackOverflow