简体   繁体   中英

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. 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.

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. 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. 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.

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.

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.

Two problems here: a. encoding, b. content display.

Encoding

Somewhere in header (or body) is a property 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 = 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. An iframe is the easiest way to display this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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