【问题标题】:Zend Mail and encodings, content-transfer etc. - Unified?Zend 邮件和编码、内容传输等 - 统一?
【发布时间】:2011-06-18 04:20:02
【问题描述】:

Zend 框架中是否有任何类可以让我轻松阅读电子邮件?

Zend_Mail 类确实让我能够轻松获取标题、主题和内容正文。但是将所有内容都转换为 UTF-8 和人类可读的格式仍然很痛苦。

还是我做错了什么?据我所知,Zend 框架不允许我轻松获取我可以使用的 UTF-8 字符串,我仍然需要进行一些后期处理。对吧?

【问题讨论】:

  • 2010年初我做了一些zend mail pop3类的实验,这真的很简单,我能够真实的邮件内容,主题和形式,我只用英文测试,请你给一个关于编码问题的例子......
  • 一旦你有不同的内容传输编码或发送带有元音变音符号的电子邮件,来自或任何其他标题你会遇到问题......有很多这样的东西:= ?ISO-8859-1?B?SmV0enQgZ/xuc3RpZyBpbiBkaWUgVVNBIGZsaWVnZW4gLSBt?= =?ISO-8859-1?B?aXQgTWVpbGVuIG9kZXIgYWIgNDQ5IEV1cm8q?= 据我所知,Zend 框架不会自动处理这个问题。
  • 这似乎是 base64_encoded 字符串,这可能有助于stackoverflow.com/questions/834152/…
  • 我使用 imap_mime_header_decode 将其转换,然后使用 iconv 将其转换为 UTF-8。但是当我从 Zend_Mail_Part 获取 Header 值时,Zend 框架不应该自动执行此操作吗?这太痛苦了……

标签: email zend-framework encoding


【解决方案1】:

关键是您需要遍历 Message 中的各个部分并找到文本。拥有它后,您可以使用quoted_printable_decode 以一种有用的方式获取文本本身。

这是一些使用 Zend_Mail 读取 IMAP 邮箱的粗略且现成的代码:

<?php
$mail = new Zend_Mail_Storage_Imap(array(
        'host'   => EMAIL_ACCOUNT_HOST,
        'user'     => EMAIL_ACCOUNT_USERNAME,
        'password' => EMAIL_ACCOUNT_PASSWORD,
    ));

echo (int)$mail->countMessages() . " messages found\n";

foreach ($mail as $message) { 

    $from = $message->getHeader('from');
    $subject = trim($message->subject);
    $to = trim($message->to);
    $body = getBody($message);

    // do something with message here
}

function getBody(Zend_Mail_Message $message)
{
    // find body
    $part = $message;
    $isText = true;
    while ($part->isMultipart()) {
        $foundPart = false;
        $iterator = new RecursiveIteratorIterator($message);
        foreach ($iterator as $part) {
            // this detection code is a bit rough and ready!
            if (!$foundPart) {
                if (strtok($part->contentType, ';') == 'text/html') {
                    $foundPart = $part;
                    $isText = false;
                    break;
                } else if (strtok($part->contentType, ';') == 'text/plain') {
                    $foundPart = $part;
                    $isText = true;
                    break;
                }
            }
        }

        if($foundPart) {
            $part = $foundPart;
            break;
        }
    }
    $body = quoted_printable_decode($part->getContent());

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2015-10-21
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2014-03-03
    • 2012-09-22
    相关资源
    最近更新 更多