PHP imap_fetchbody

PHP imap_fetchbody,php,imap,gmail-imap,Php,Imap,Gmail Imap,我一直试图获取消息,但未成功 $body = imap_fetchbody($inbox, $email_id, 0); 没有附件的邮件很好,我有输出,但有附件 给出了一些复杂的输出,其中html和普通邮件都使用gmail邮件的一部分(内容类型)进行编码您可以使用以下代码获取多部分电子邮件正文的纯文本部分: <?php //get the body $body = imap_fetchbody($inbox, $email_id, 0); //parse the boundary s

我一直试图获取消息,但未成功

$body = imap_fetchbody($inbox, $email_id, 0);
没有附件的邮件很好,我有输出,但有附件
给出了一些复杂的输出,其中html和普通邮件都使用gmail邮件的一部分(内容类型)进行编码

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

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

这很有帮助

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


这似乎是唯一一个适合我的。我认为最后一个参数中的第一个整数代表电子邮件的部分,因此如果它以零开头,它将包含所有的标题信息。如果以1开头,则包含消息信息。然后第二个整数后跟句点就是该段的段。因此,当我将0置为零时,它显示信息,但当我将1或2置为零时,它不会显示某些电子邮件的任何内容。

带有附件的邮件通常是多部分的,用边界字符串分隔。你到底想干什么?你只是想得到短信的文本部分吗?是的,我只需要短信。这在所有情况下都不起作用。MIME非常灵活。我尝试过你的代码,但是列表(,$boundary)=$matches;存在未定义偏移的错误:1更多详细信息:。
$body = imap_fetchbody($inbox, $email_id, 1.0);