Php 如何向网站显示电子邮件的内容?
我想显示电子邮件的正文内容。我在php中尝试过IMAP,但有些地方很不对劲。IMAP没有接收我邮件的正文。它只拾取身体上的签名。因此,我正在寻找阅读电子邮件正文内容到网页的替代方法 这是我电子邮件的原始文档: IMAP正在抓取免责声明/版权模糊,但正文中没有显示任何其他内容。有人有其他方法可以从gmail或任何其他网站上阅读电子邮件,并将内容显示到网页上吗 我已经放弃让IMAP阅读它,因为没有人能够解决这个问题…我已经花了几个小时,所以我放弃了,但这是代码Php 如何向网站显示电子邮件的内容?,php,email,imap,Php,Email,Imap,我想显示电子邮件的正文内容。我在php中尝试过IMAP,但有些地方很不对劲。IMAP没有接收我邮件的正文。它只拾取身体上的签名。因此,我正在寻找阅读电子邮件正文内容到网页的替代方法 这是我电子邮件的原始文档: IMAP正在抓取免责声明/版权模糊,但正文中没有显示任何其他内容。有人有其他方法可以从gmail或任何其他网站上阅读电子邮件,并将内容显示到网页上吗 我已经放弃让IMAP阅读它,因为没有人能够解决这个问题…我已经花了几个小时,所以我放弃了,但这是代码 <?php /* co
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'username@gmail.com';
$password = 'password';
/* 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, 2);
echo $message;
echo imap_qprint($message);
$message = imap_qprint($message);
echo imap_8bit ($message);
$DateFormatted = str_replace("-0500", "", $overview[0] -> date);
/* output the email header information */
$output .= $overview[0] -> subject ;
$output .= $DateFormatted ;
//$bodyFormatted = preg_replace("/This e-mail(.*)/","",$message);
//$bodyFormatted = preg_replace("/Ce courriel(.*)/","",$bodyFormatted);
/* output the email body */
$output .= $message;
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
Gmail有一些不同的IMAP设置,请更仔细地遵循原始代码:
您正在处理由多个部分组成的邮件(查看您的pastebin电子邮件示例)。 作为测试,请尝试使用以下行:
$message = imap_fetchbody($inbox, $email_number, "1.1");
纯文本版本生活在1.1之下
HTML版本是1.2
签名在下一部分-它是2。这是您在代码示例中检索到的内容。您有权访问原始电子邮件内容(包含所有标题的内容等) 如果是,请尝试使用 我以前用过,效果很好
$emailParser = new PlancakeEmailParser(...raw email content...);
$emailTo = $emailParser->getTo();
$emailSubject = $emailParser->getSubject();
$emailCc = $emailParser->getCc();
$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To');
$emailBody = $emailParser->getPlainBody();
$emailHtml = $emailParser->getHTMLBody();
除了DmitryK的建议 添加下面的符号可以使一切正常工作,没有随机的“=”符号。Str_replace用于删除在页面上生成的“=”s
$message = imap_fetchbody($inbox, $email_number, "1.1");
$message = str_replace("=", "", $message);
我不知道为什么“=”是100%随机生成的,但这很可能是由于Exchange服务器的某些加密问题造成的,因为我们的服务器已经使用了大约10年。您可以发布您试图获取消息的
IMAP
代码吗?我有完全相同的代码,它不适用于我的电子邮件头或从exchange服务器发送到gmail帐户的电子邮件。IMAP只能读签名。但是,如果我将相同的电子邮件内容从gmail或电子邮件发送到gmail帐户,它就会读取这些内容……这与我从exchange server发送到gmail的电子邮件的格式或标题有关。我在我的帖子中的pastebin链接上发布的那些标题与Exchange server不同,它不完全符合IMAP标准。在这种情况下,我该怎么办?总而言之,gmail对我来说是有效的,但exchange server gmail不起作用。。gmail意味着发送到gmail帐户的gmail电子邮件。。或者yahoo gmail意思是yahoo电子邮件发送到gmail帐户谢谢,我使用的是“$message=imap_fetchbody($inbox,$email_number,“1.1”);”这就是为什么除了我的正文之外,我还获得了随机的标题,添加了.1修复了标题,但“=”s仍然存在。请确保使用utf-8。如果是这种情况,我建议您改用quoted_printable_decode()。它将删除“=”。否则,您还必须将其编码为utf-8。utf-8_编码(引用可打印_解码());