当前位置: 首页 > 知识库问答 >
问题:

无法从Gmail获取IMAP中的邮件正文

赫连实
2023-03-14

我想看GMail的邮件正文。

我能够获得所有详细信息,例如发件人姓名,电子邮件和主题等,但不能获得电子邮件正文。

// Connect to gmail
$username = 'email';
$password = 'password';
/* connect to server */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
/* try to connect */
$inbox = imap_open($hostname, $username, $password) or die('Cannot connect to Gmail: ' . imap_last_error());
/* grab emails */
$emails = imap_search($inbox, 'ALL');
/* if emails are returned, cycle through each... */
if ($emails) {
    /* begin output var */
    $output = '';
    /* put the newest emails on top */
    rsort($emails);
    /* for every email... */
    foreach ($emails as $email_number) {
        /* get information specific to this email */
        $overview = imap_fetch_overview($inbox, $email_number, 0);
        $message = imap_fetchbody($inbox, $email_number, "1.2");

        /* output the email header information */
        $output.= '<div class="toggler ' . ($overview[0]->seen ? 'read' : 'unread') . '">';
        $output.= '<span class="subject">' . $overview[0]->subject . '</span> ';
        $output.= '<span class="from">' . $overview[0]->from . '</span>';
        $output.= '<span class="date">on ' . $overview[0]->date . '</span>';
        $output.= '</div>';

        /* output the email body */
        $output.= '<div class="body">' . $message . '</div>';
    }

    echo $output;
}

我知道有特定的Gmail的API可以做到这一点,但IMAP不应该工作吗?

从PHP的文档中可以看出:

(empty) - Entire message
0 - Message header
1 - MULTIPART/ALTERNATIVE
1.1 - TEXT/PLAIN
1.2 - TEXT/HTML
2 - MESSAGE/RFC822 (entire attached message)
2.0 - Attached message header
2.1 - TEXT/PLAIN
2.2 - TEXT/HTML
2.3 - file.ext

我尝试了<code>$message=imap_fetchbody($inbox,$email_number,“1.2”)和$message=imap_fetchbody($inbox,$email_number,1.2)

但它返回空结果。

我还尝试了$message=imap_fetchbody($inbox,$email_number,“2”)但它返回的HTML与我在Gmail中得到的非常不同。


共有2个答案

皮弘博
2023-03-14

我已经尝试并成功了

$message = quoted_printable_decode(imap_fetchbody($conn, $email_number, 1));
燕琨
2023-03-14

我不得不使用的下车

$message=quoted_printable_decode(imap_fetchbody($inbox,$email_number,2))

以正确获取消息。

 类似资料:
  • 我能够通过IMAP连接到Gmail并进行解析,但是在电子邮件正文中,我获得了Gmail特定的内容,例如

  • 问题内容: 我在使用Gmail PHP API时遇到问题。 我想检索电子邮件的正文内容,但是我只能对具有附件的电子邮件检索它!我的问题是为什么? 到目前为止,这是我的代码: 检索我知道的内容的第二种方法是使用位于“ 标题” 部分中的代码段,但它只能检索50个左右的前字符,这不是很有用。 问题答案: 让我们做一个小实验。我已经给自己发了两条消息。一个带有附件,一个没有附件。 请求: 响应: 我只要求

  • 我一直试图获取电子邮件正文与imap_fetchbody($stream,$msgno,$选项),但没有成功。 然后我尝试使用imap_fetchstructure($stream,$msgno),并使用各自的子类型手动解码每种类型,如:- 对于前两个,我可以用这样的东西来解码 但说到混合,我要么不知道,要么我该怎么办,要么我在哪里犯错。

  • 问题内容: 我正在尝试阅读我的消息,我可以获取它来打印标题,但发件人和内容显示得很滑稽。这是我用来显示消息的代码: 输出如下: 为什么不打印出from语句的实际电子邮件地址?为什么不打印出实际的身体内容?(我只是在纯文本中很有趣。) 整个代码: 谢谢! 问题答案: 对于纯文本和html消息: 对于分段消息:

  • 我连接到outlook。Office 365。com通过库IMAPClient(Python)同步电子邮件。通过IDLE mechanizm,我在服务器上接收更改,例如在文件夹收件箱中接收新电子邮件。之后,我获取UID为{last_synced_UID}:*的邮件——这应该会给出上次同步之后的所有邮件(UID总是递增的)。 此流程可以工作,但IMAP服务器(outlook.office365.co

  • 问题内容: 我正在尝试在JavaMail API的帮助下通过IMAP访问来自Gmail帐户的电子邮件。我想知道为什么代码对一个电子邮件帐户有效,而对另一个电子邮件帐户无效。 我可以访问两个电子邮件帐户的文件夹。但是对于其中一个电子邮件帐户,无法访问其他文件夹,例如,它将引发异常。有人可以解释出什么问题了吗? 先感谢您。 这是代码: 问题答案: 是否有一个帐户使用非英语用户界面? Gmail文件夹名