簡體   English   中英

解析的HTML電子郵件部分將不會顯示在瀏覽器中

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

當客戶從特定服務在Web上收到評論時,該服務會發送並通過電子郵件通知新評論。 電子郵件的內容包括評論本身和源信息,內容為(電子郵件標題,電子郵件正文第1部分(純文本)和電子郵件正文第2部分(HTML)。解析這些電子郵件的HTML部分,並將其推送到新的Flatfile中,以供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);

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-如果適用,具有較高代表的人可以將imap-fetchbody添加到標簽列表中嗎,它不會允許我。

這里有兩個問題: 編碼,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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM