PHP imap_fetchbody

时间:2015-03-25 12:46:40

标签: php imap gmail-imap

我一直在尝试获取消息但不成功。

$body = imap_fetchbody($inbox, $email_id, 0);

没有附件的邮件很好,我有输出但有附件 给出了一些复杂的输出,其中html和普通消息都用一些(Content-Type)编码,这是gmail消息的一部分

3 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取多部分电子邮件正文的纯文本部分:

<?php

//get the body
$body = imap_fetchbody($inbox, $email_id, 0);

//parse the boundary separator
$matches = array();
preg_match('#Content-Type: multipart\/[^;]+;\s*boundary="([^"]+)"#i', $body, $matches);
list(, $boundary) = $matches;

$text = '';
if(!empty($boundary)) {
    //split the body into the boundary parts
    $emailSegments = explode('--' . $boundary, $body);

    //get the plain text part
    foreach($emailSegments as $segment) {
        if(stristr($segment, 'Content-Type: text/plain') !== false) {
            $text = trim(preg_replace('/Content-(Type|ID|Disposition|Transfer-Encoding):.*?\r\n/is', '', $segment));
            break;
        }
    }
}

echo $text;
?> 

答案 1 :(得分:0)

这有帮助

  

$ body = imap_fetchbody($ inbox,$ email_id,1.1);

答案 2 :(得分:0)

$body = imap_fetchbody($inbox, $email_id, 1.0);

这似乎是唯一为我工作的人。我认为最后一个参数中的第一个整数代表电子邮件的一部分,因此,如果以零开头,它将包含所有标头信息。如果以1开头,则包含消息信息。然后,第二个整数后跟句点是该部分的部分。因此,当我置零时,它显示信息,但是当我置一或二时,对于某些电子邮件它什么都不显示。

相关问题