当客户端从特定服务收到Web审核时,该服务会发送通知新审核的电子邮件。电子邮件的内容包括评论本身和源信息,内容如(电子邮件标题,电子邮件正文第1部分(纯文本)和电子邮件正文第2部分(HTML)。我需要解析这些电子邮件的HTML部分,并将它们推送到客户推荐页面上的PHP新平面文件中。
我已成功连接到我的邮件服务,并解析我正在测试的测试电子邮件的第2部分。问题是,当在浏览器中查看输出时,它只是空白。但是当我查看源代码时 - 包含HTML代码/结构/ css的所有内容都存在。我不知道为什么我在浏览器的前端看不到任何内容(plaint text或HTML),但在源视图中看到了所有内容。
这是我的代码:
$login="*email user name*";
$password="*email password*";
$connection = imap_open('{pop.secureserver.net:995/novalidate-cert/pop3/ssl}', $login, $password);
$count = imap_num_msg($connection); /* get number of messages on server */
$i = 46; /* message 46 is the message being used to test this */
$header = imap_header($connection, $i);
$body = imap_fetchbody($connection,$i,"2"); /* grab section 2 of e-mail (HTML) */
$prettydate = date("F jS Y", $header->udate); /* not necessary just part of testing response */
if (isset($header->from[0]->personal)) {
$personal = $header->from[0]->personal;
} else {
$personal = $header->from[0]->mailbox;
}
$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";
echo "On $prettydate, $email said <hr>";
echo $body;
echo "<hr>";
imap_close($connection);
来自Chrome内部的PHP响应之后的前15行“查看源代码”。
On August 3rd 2014, New Review Notifications <pl-no-reply@reviews.com> said <hr><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.=
w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=3D"http://www.w3=
.org/1999/xhtml"> <head> <meta http-equiv=3D"Content-Type" content=3D"text/=
html; charset=3Dutf-8" /> <title></title> <style type=3D"text/css"> .Extern=
alClass{display:block !important;} .yshortcuts, .yshortcuts a, .yshortcuts =
a:link, .yshortcuts a:visited, .yshortcuts a:hover, .yshortcuts a span{ col=
or:#008ec5; text-decoration:none !important; border-bottom:none !important;=
background:none !important; } body{margin:0;} p{margin:0 !important;} </st=
yle> </head> <body marginheight=3D"0" marginwidth=3D"0" leftmargin=3D"0" to=
pmargin=3D"0" bgcolor=3D"#ffffff"> <table width=3D"100%" cellpadding=3D"0" =
cellspacing=3D"0" bgcolor=3D"#ffffff"> <tr> <td height=3D"25" style=3D"back=
ground-color: #88939B" colspan=3D"3"></td> </tr> <tr> <td width=3D"50%" val=
ign=3D"top"> <table width=3D"100%" cellpadding=3D"0" cellspacing=3D"0" styl=
e=3D"height: 232px;"> <tr> <td style=3D"background-color: #88939B; height: =
232px; display: block"> </td> </tr> </table> </td> <td width=3D"632"> =
PS - 有更高代表的人可以将imap-fetchbody添加到标记列表中(如果适用),它不会让我。
答案 0 :(得分:0)
这里有两个问题:a。编码,b。内容显示。
<强>编码
标题(或正文)中的某个位置是属性encoding
。
这告诉您邮件内容是如何编码的。
然后,您可以使用这些信息来还原编码。
它可能是$body->encoding
,而不是$header->encoding
。
$body = imap_fetchbody($connection,$i,"2");
if ($header->encoding == 4) {
$body = quoted_printable_decode($body);
}
if ($header->encoding == 3) {
$body = base64_decode($body);
}
echo $body; // now body should have the correct encoding
也试一试:
$body = imap_fetchbody($connection,$i, 1.2); <-- instead of 2
内容显示
正如Marc B已经指出的那样,如果没有iframe,就无法在HTML页面内呈现完整的HTML页面。 iframe是显示此内容的最简单方法。
但是你在这里有几个选择,从身体提取的标签去除。
如果删除“重要”标记,则会获得内容。
<body>.*</body>
的preg_matching也应该有效。
$body = str_replace('<html>', '', $body);
$body = str_replace('<head>', '', $body);
$body = str_replace('<title>', '', $body);
$body = str_replace('<body>', '', $body);
$body = str_replace('</body>', '', $body);
$body = str_replace('</html>', '', $body);