2012-07-17 74 views
0

我正在爲Gmail構建一個基本的只讀客戶端,我成功地通過imap(以PHP)拉電子郵件並存儲它們,但是我在瀏覽器中顯示它們時遇到了一些問題。在另一個包裝完整的HTML頁面

大多數電子郵件都是完整的HTML頁面<html><title>Your Mail</title><body>etc...</body></html>這經常會導致電子郵件顯示在根html頁面中斷。

比如你經常落得

<html> 
<title>Read only mail> 
<body> 
<h1>Your mail:</h1> 
<html><title>An email from steve</title><style>html {background-color=red;}</style> 
<body>It's a read email! (get it?)</body></html> 
<html><title>An email from james</title><style>body {background-color=green;}</style> 
<body>I like green things, save the planet!</body></html> 
More mail etc... 
</body> 
</html> 

這是不是完全有效的HTML,但主要問題是郵件的CSS會影響頁面上的所有其他元素,我的最好的辦法就是以某種方式使一個帶有PHP的iframe(這可能不需要進一步的請求),或者以某種方式去除文檔中的所有CSS和HTML。

有誰知道如何解決這個問題? gmail和其他網絡客戶端如何做到這一點? 快樂尋找到客戶端的解決方案,例如Javascript/jQuery的&阿賈克斯

感謝您的時間,

-

目前該郵件是隻輸出這樣:

/* connect */ 
$inbox = imap_open($hostname,$username,$password) or die(imap_last_error()); 

$emails = imap_search($inbox,'ALL'); 
if($emails) { 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

    /* get information specific to this email */ 
    $overview = imap_fetch_overview($inbox,$email_number,0); 
    $message = imap_fetchbody($inbox,$email_number,2); 

    /* output the email header information */ 
    $output.= '<div class="toggler '.($overview[0]->seen ? 'read' : 'unread').'">'; 
    $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
    $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
    $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
    $output.= '</div>'; 

    /* output the email body */ 
    $output.= '<div class="body">'.$message.'</div>'; 
    } 

    echo $output; 
} 

/* close the connection */ 
imap_close($inbox) 

取自http://davidwalsh.name/gmail-php-imap(我的代碼略有不同,但設計相同&輸出)

+0

使用DOM文檔或S如何提取身體標籤之間的什麼。 – 2012-07-17 08:29:14

+1

對於此電子郵件功能,您可以呈現純文本版本的電子郵件,而不是HTML。這會爲你節省很多麻煩! – halfer 2012-07-17 08:32:46

+0

任何想法如何提取純文本版本/如何請求純文本與imap? – 2012-07-17 08:38:25

回答

0

這裏試試這個,與DOM文檔中提取最新的每個消息的body標籤之間:

<?php set_time_limit(0); ?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>You have mail</title> 
</head> 

<body> 

<?php 
/* connect */ 
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
$username = ''; 
$password = ''; 
$inbox = imap_open($hostname,$username,$password) or die(imap_last_error()); 

$emails = imap_search($inbox,'ALL'); 
if($emails){ 

    /* begin output var */ 
    $output = ''; 

    /* put the newest emails on top */ 
    rsort($emails); 

    /* for every email... */ 
    foreach($emails as $email_number) { 

     /* get information specific to this email */ 
     $overview = imap_fetch_overview($inbox, $email_number, 0); 
     $message = imap_fetchbody($inbox, $email_number, 2); 

     /* output the email header information */ 
     $output.= '<div class="toggler'.($overview[0]->seen ? 'read' : 'unread').'">'; 
     $output.= '<span class="subject">'.$overview[0]->subject.'</span> '; 
     $output.= '<span class="from">'.$overview[0]->from.'</span>'; 
     $output.= '<span class="date">on '.$overview[0]->date.'</span>'; 
     $output.= '</div>'; 

     /* output only the email body */ 
     $dom = new DOMDocument(); 
     libxml_use_internal_errors(true); 
     $dom->loadHTML($message); 
     $body = $dom->getElementsByTagName("body")->item(0); 
     if ($body !== null) { 
      for ($n = $body->firstChild; $n !== null; $n = $n->nextSibling) { 
       $body = simplexml_import_dom($n)->asXML(); 
      } 
     } 
     $output.= '<div id="body">'.$body.'</div>'; 
    } 

    echo $output.' 
</body> 
</html>'; 
} 

/* close the connection */ 
imap_close($inbox); 
?> 

爲了回答您的其他問題,如果你只需要電子郵件的純文本部分(壽這也可能包含多邊界)

使用$message = imap_fetchbody($inbox, $email_number, 1);通知1

希望它可以幫助

0

爲什麼不選擇你想要保留的元素,比如通過php的身體本身,然後放棄其餘的?您當然需要切換到適用於一組基本電子郵件內容佈局的不同規則:但您可以自由地爲每個佈局實施特定和可靠的規則。

0

您可以使用iframe來顯示消息,或使用正則表達式服務器端抓取body標籤內的文本或使用dom客戶端抓取body元素。

+1

沒有魔法...... – Ali 2012-07-17 08:34:57