2011-12-02 119 views
4

我想顯示電子郵件的正文內容。我曾嘗試在PHP中使用IMAP,但有些事情是非常錯誤的。 IMAP沒有提供我的信息的正文。它只是拿起身上的簽名。因此,我正在尋找將電子郵件正文內容讀取到網頁的其他方法。如何顯示電子郵件的內容到網站?

這裏是我的電子郵件的原始文件:

http://pastebin.com/WQra335P

免責聲明/版權模糊正在被顯示IMAP,但沒有在身體其他抓起。任何人都可以選擇從Gmail或任何其他可以將內容顯示到網頁的網站閱讀電子郵件?

我已經放棄了使IMAP讀它,因爲沒有人能弄清楚這個問題......我花了幾個小時,所以我放棄了,但這裏是代碼...

<?php 
    /* connect to gmail */ 
    $hostname = '{imap.gmail.com:993/imap/ssl}INBOX'; 
    $username = '[email protected]'; 
    $password = 'password'; 

    /* try to connect */ 
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); 

    /* grab emails */ 
    $emails = imap_search($inbox,'ALL'); 

    /* if emails are returned, cycle through each... */ 
    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); 
      echo $message; 
      echo imap_qprint($message); 
      $message = imap_qprint($message); 
      echo imap_8bit ($message); 
      $DateFormatted = str_replace("-0500", "", $overview[0] -> date); 

      /* output the email header information */ 
      $output .= $overview[0] -> subject ; 
      $output .= $DateFormatted ; 

      //$bodyFormatted = preg_replace("/This e-mail(.*)/","",$message); 
      //$bodyFormatted = preg_replace("/Ce courriel(.*)/","",$bodyFormatted); 
      /* output the email body */ 
      $output .= $message; 

     } 

    echo $output; 

    } 

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

?> 
+2

你可以發佈你試圖獲取消息的'IMAP'代碼嗎? – Cyclonecode

回答

2

除了什麼DmitryK建議,

添加下面讓一切正常工作沒有隨機「=」的跡象。 Str_replace用於刪除在頁面上生成的「=」。

$message = imap_fetchbody($inbox, $email_number, "1.1"); 
$message = str_replace("=", "", $message); 

我不知道爲什麼是隨機生成的「=」 S 100%,但是這很可能是由於從Exchange Server的身邊一些加密問題,因爲我們的服務器是10歲左右。

0

Gmail中有一些不同的IMAP設置,按照原來的代碼更加緊密:

http://davidwalsh.name/gmail-php-imap

+0

我有完全相同的代碼,它不適用於我的電子郵件標題或從交換服務器到Gmail帳戶的電子郵件。 IMAP無法讀取除簽名外的任何內容。但是,如果我採用相同的電子郵件內容並通過電子郵件將它們從gmail或電子郵件發送到gmail帳戶,那麼它會讀取它們......這與我的電子郵件的格式或標題(來自交換服務器到Gmail)有關。這些標題是我發佈在我的帖子的pastebin鏈接上面 – Bulvak

+0

Exchange服務器是不同的野獸,它完全不符合IMAP標準。 – 2011-12-05 17:15:30

+0

在這種情況下我能做些什麼?總結它爲我的gmail-gmail工作,但交換server-gmail does not work .. gmail-gmail意思是發送到gmail帳戶的gmail電子郵件..或yahoo-gmail的意思yahoo電子郵件發送到gmail帳戶 – Bulvak

2

您正在處理的多部分消息(看你的引擎收錄的電子郵件樣本)。 作爲測試嘗試使用這一行:下1.1 HTML版本

$message = imap_fetchbody($inbox, $email_number, "1.1"); 

純文本版本居住爲1.2 簽名是在未來的一部分 - 它是2,這是你的代碼樣本中獲取什麼。

+0

謝謝,我正在使用「$ message = imap_fetchbody($ inbox,$ email_number,」1.1「);」這就是爲什麼我除了我的身體文本,添加.1固定標題,但「=」仍然存在。 – Bulvak

0

你有訪問原始的電子郵件內容

如果是這樣(與所有頭等等的東西),請嘗試使用plancacke email parser

我有很好的成功使用過。

$emailParser = new PlancakeEmailParser(...raw email content...); 

$emailTo = $emailParser->getTo(); 
$emailSubject = $emailParser->getSubject(); 
$emailCc = $emailParser->getCc(); 
$emailDeliveredToHeader = $emailParser->getHeader('Delivered-To'); 
$emailBody = $emailParser->getPlainBody(); 
$emailHtml = $emailParser->getHTMLBody(); 
相關問題