2013-01-05 36 views
-1

可能重複:
PHP imap problems幫我用php imap?

我需要能夠從PHP腳本中使用我的電子郵件(Gmail的)。但是,無論我在哪裏嘗試,電子郵件正文都會出現垃圾,字符類似= 3D和隨機等號。有時它以base64形式出現,或者根本沒有。如果沒有可用的html,我怎樣才能得到一封電子郵件並將其顯示在HTML Purifier乾淨的html或預標記中的純文本中。

$overview = imap_fetch_overview($inbox,$email_number,0); 

$body_pre = imap_fetchbody($inbox, $email_number, 2.1); 

$message = imap_fetchbody($inbox, $email_number, 2.2); 

$message = base64_decode($message); 

if (empty($message)){ 

    $message = $body_pre; 
    $message = preg_replace('@(https?://([-\w\.]+)+(:\d+)?((/[\w/_\.%\-+~]*)?(\?\S+)?)?)@', '<a href="$1">$1</a>',$message); 
    $message = '<pre>'.htmlentities($message).'</pre>'; 

}else{ 

    $cleanconfig = HTMLPurifier_Config::createDefault(); 
    $cleanconfig->set('Core.Encoding', 'UTF-8'); 
    $cleanconfig->set('HTML.Doctype', 'HTML 4.01 Transitional'); 
    $purifier = new HTMLPurifier($cleanconfig); 

    $message = $purifier->purify($message); 

} 

這個代碼,$消息只自帶空白。

+0

當你用最後一個參數調用imap_fetchbody()時會發生什麼? – vodich

+0

與1我得到整個郵件的純文本和HTML與內容類型標題 – user1948720

+0

不要嘗試處理直接使用PHP調用電子郵件 - 郵件是很難處理。使用一個完善的庫 - 有一些PHP。也許Swiftmailer會這樣做? http://swiftmailer.org/ – halfer

回答

1

帶有「字符類似= 3D和隨機等號」的消息是引用可打印的編碼。你可以使用php解碼quoted_printable_decode。

郵件消息的編碼方式有多種,如quited-printable或base64。發件人決定使用的編碼。