解析后的HTML电子邮件部分不会显示在浏览器中


Parsed HTML e-mail section wont display in browser

当客户端在web上收到来自特定服务的评论时,该服务会发送电子邮件通知新的评论。电子邮件的内容包括审查本身和源信息,内容包括(电子邮件头、电子邮件正文PART 1(纯文本)和电子邮件正文PART 2 (HTML))。我需要解析这些电子邮件的HTML部分,并将它们推送到一个新的平面文件,以便在客户推荐页面上包含PHP。

我成功地连接到我的邮件服务,并解析我用来测试的测试电子邮件的第2部分。问题是,当在浏览器中查看输出时,它只是空白。但是当我查看源代码时,所有的内容包括HTML代码/结构/css都在那里。我不知道为什么我在浏览器的前端什么也看不见(抱怨文本或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);

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">&nbsp;</td> </tr> </table> </td> <td width=3D"632"> =

PS -可以有人与更高的代表添加imap-fetchbody标签列表,如果适用,它不会让我。

这里有两个问题:a.编码,b.内容显示。

<<p> 编码/strong>

在header(或body)的某处是一个属性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提取。如果你去掉"重要"标签,你就得到了内容。preg_matching for <body>.*</body>也可以。

$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);