Zend Mail 和编码、内容传输等 - 统一吗?

发布于 2024-10-14 18:03:14 字数 216 浏览 2 评论 0原文

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

Zend_Mail 类确实允许我轻松获取标题、主题和内容正文。但是将所有内容转换为 UTF-8 和人类可读的格式仍然是一件痛苦的事情。

或者我做错了什么?据我所知,Zend Framework 不允许我轻松获取我可以使用的 UTF-8 字符串,我仍然需要进行一些后处理。正确的?

Is there any class in the Zend Framework that allows me to easily read emails?

The Zend_Mail class does allow me to easy get headers, subject and the content body. But transferring everything to UTF-8 and human-readable format is still a pain.

Or am I doing something wrong? As far as I can tell, Zend Framework does not allow me to easily get UTF-8 strings that I can just use, I still have to do some post-processing. Right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以心动 2024-10-21 18:03:14

关键是您需要迭代消息中的各个部分并找到文本。一旦获得它,您就可以使用 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());

}

The key thing is that you need to iterate over the parts within the Message and find the text. Once you have it, then you can use quoted_printable_decode to get the text itself in a useful way.

This is some rough and ready code that reads IMAP email boxes with Zend_Mail:

<?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());

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文