简体   繁体   English

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

[英]Parsed HTML e-mail section wont display in browser

When a client receives a review on the web from a specific service, that service sends and e-mail that notifies of the new review. 当客户从特定服务在Web上收到评论时,该服务会发送并通过电子邮件通知新评论。 The content of the e-mail includes the review itself, and source information, the content comes as (e-mail header, e-mail body PART 1(plain text) and e-mail body PART 2 (HTML). I need to parse the HTML section of those e-mails and push them out to a new flatfile for PHP include on the clients testimonials page. 电子邮件的内容包括评论本身和源信息,内容为(电子邮件标题,电子邮件正文第1部分(纯文本)和电子邮件正文第2部分(HTML)。解析这些电子邮件的HTML部分,并将其推送到新的Flatfile中,以供PHP包含在客户推荐页面上。

I am successfully able to connect to my mail service, and parse section 2 of the test e-mail i am using to test with. 我可以成功连接到我的邮件服务,并解析要用于测试的测试电子邮件的第2部分。 the problem is that when the output is viewed in the browser it's simply blank. 问题是,当在浏览器中查看输出时,它只是空白。 But when I view source - all of the content inclusive of HTML code/structure/css is there. 但是,当我查看源代码时-包括HTML代码/结构/ css在内的所有内容都在那里。 I'm at a loss as to why I see nothing (plaint text or HTML) on the front end of the browser but see everything in source view. 为什么我在浏览器的前端看不到任何东西(纯文本或HTML),却在源代码视图中看到所有东西,我茫然无措。

Here is my code: 这是我的代码:

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

First 15 lines of "view source" after PHP response from within chrome. 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">&nbsp;</td> </tr> </table> </td> <td width=3D"632"> =

PS - can someone with higher rep add imap-fetchbody to tag list if applicable, it wont let me. PS-如果适用,具有较高代表的人可以将imap-fetchbody添加到标签列表中吗,它不会允许我。

Two problems here: a. 这里有两个问题: encoding, b. 编码,b。 content display. 内容显示。

Encoding 编码方式

Somewhere in header (or body) is a property encoding . 标头(或正文)中的某处是属性encoding This tell's you how your mail content is encoded. 告诉您邮件内容的编码方式。 You can then use these pieces of information to revert the encoding. 然后,您可以使用这些信息来还原编码。 It might be $body->encoding , instead of $header->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

Give this a try, too: 也尝试一下:

$body = imap_fetchbody($connection,$i, 1.2); <-- instead of 2

Content Display 内容显示

As Marc B already pointed out, it's not possible to render a complete HTML page inside a HTML page, without an iframe. 正如Marc B已经指出的那样,如果没有iframe,则无法在HTML页面内呈现完整的HTML页面。 An iframe is the easiest way to display this. iframe是显示此内容的最简单方法。

But you have several options here, from tag-removal over body-extraction. 但是,在这里您有几种选择,从标签去除到身体提取。 If you remove the "important" tags, you get the content. 如果删除“重要”标签,则会得到内容。 preg_matching for <body>.*</body> should work, too. <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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM