提取邮件正文 - 当有附件时

时间:2013-12-30 03:51:45

标签: php email sendmail email-parsing

我需要在php中提取邮件正文。

目前我正在使用mimemailparser“getmessagebody”函数来检索邮件正文。

当没有任何其他(.msg)文件的邮件有附件时,它运行良好,但如果邮件包含附件也是.msg格式的邮件,它会获得附件的主体而不是当前邮件的正文。使用以下功能和代码。

public function getMessageBody($type = 'text') {
        $body = false;
        $mime_types = array(
            'text'=> 'text/plain',
            'html'=> 'text/html'
        );
        if (in_array($type, array_keys($mime_types))) {
            foreach($this->parts as $part) {
                if ($this->getPartContentType($part) == $mime_types[$type]) {
                    $headers = $this->getPartHeaders($part);
                    $body = $this->decode($this->getPartBody($part), array_key_exists('content-transfer-encoding', $headers) ? $headers['content-transfer-encoding'] : '');
                }
            }
        } else {
            throw new Exception('Invalid type specified for MimeMailParser::getMessageBody. "type" can either be text or html.');
        }
        return $body;
    }

代码:

$this->parsed->setText($this->mail);
$this->message = $this->parsed->getMessageBody('text');

问题:getmessagebody检索附加邮件的正文而不是原始邮件。任何解决方案?

1 个答案:

答案 0 :(得分:0)

找到这个问题的答案。 getmessagebody - >实际上它总是返回最后的匹配部分而不是第一个(它可能有文本/纯文本消息和文本/普通附件)。

http://code.google.com/p/php-mime-mail-parser/issues/attachmentText?id=10&aid=100004000&name=getMessageBody.txt&token=vCOlNmTnFSlEGmUs31i5tY_KkoQ%3A1388629941070