我想阅读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用于执行此操作但不应该使用IMAP吗?
来自PHP&#39; documentation它说
(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
我尝试了$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中获得的HTML完全不同。
答案 0 :(得分:3)
下车我必须使用
$message = quoted_printable_decode(imap_fetchbody($inbox, $email_number, 2));
让消息正确。
答案 1 :(得分:0)
我已经尝试并成功了
$message = quoted_printable_decode(imap_fetchbody($conn, $email_number, 1));