2012-06-02 75 views
4

我正在使用IMAP函數從特定郵件ID讀取郵件。但我無法讀取plain text郵件的郵件內容。這對HTML Mails來說是完美的。運行代碼後,所有plain text郵件仍然爲未讀郵件,並且標記爲已讀的html郵件以及發件人郵件標識和主題等其他內容可以讀取。只有閱讀內容時存在問題。這裏是一個我試圖無法使用IMAP獲取純文本郵件內容

include('imap.php'); 
    $hostname = '{xxx.org:143/novalidate-cert}INBOX'; 
    $username = '[email protected]'; 
    $password = 'xxxxx'; 
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to : ' . imap_last_error()); 
    $emails = imap_search($inbox,'UNSEEN'); 

    if($emails) { 
     $output = ''; 
     rsort($emails); 
     foreach($emails as $email_number) { 
     $structure = imap_fetchstructure($inbox, $email_number); 
      $savedir = dirname(__FILE__).'/uploads/'; 
        $attachments = array(); 
        if(isset($structure->parts) && count($structure->parts)) { 

         for($i = 0; $i < count($structure->parts); $i++) { 

          $attachments[$i] = array(
           'is_attachment' => false, 
           'filename' => '', 
           'name' => '', 
           'attachment' => '' 
          ); 
          if($structure->parts[$i]->ifdparameters) { 
           foreach($structure->parts[$i]->dparameters as $object) { 
            if(strtolower($object->attribute) == 'filename') { 
             $attachments[$i]['is_attachment'] = true; 
             $attachments[$i]['filename'] = $object->value; 
            } 
           } 
          } 

          if($structure->parts[$i]->ifparameters) { 
           foreach($structure->parts[$i]->parameters as $object) { 
            if(strtolower($object->attribute) == 'name') { 
             $attachments[$i]['is_attachment'] = true; 
             $attachments[$i]['name'] = $object->value; 
            } 
           } 
          } 

          if($attachments[$i]['is_attachment']) { 
           $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); 
           if($structure->parts[$i]->encoding == 3) { // 3 = BASE64 
            $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); 
           } 
           elseif($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE 
            $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); 



           } 

           $savepath = $savedir . $attachments[$i]['filename']; 
           file_put_contents($savepath, $attachments[$i]['attachment']); 

          } 
         } 
        } 





       $name = $structure->parts[1]->dparameters[0]->value; 
       $overview = imap_fetch_overview($inbox,$email_number,0); 
       $msg = imap_fetchbody($inbox,$email_number,1.2); 

       $message=''; 
       if($msg=='') 
       { 
        $message = imap_fetchbody($inbox,$email_number,2.0); 


       }else{ 
       $message=$msg; 

       } 


       $sub=$overview[0]->subject; 
        $from=$overview[0]->from; 
        $arr = explode('<', $from); 
        $from_mail = $arr[1]; 
        if($from_mail!='') 
        { 
         $from=str_replace('>','',$from_mail); 
        } 


       $randstr=''; 
       srand((double)microtime()*1000000); 

       $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
       while(strlen($randstr)<5) { 
        $randstr.=substr($chars,(rand()%(strlen($chars))),1); 
       } 

任何一個請幫助我的代碼...在此先感謝

回答

5

您的問題是這一行:

$msg = imap_fetchbody($inbox,$email_number,1.2); 

1.2 TEXT/HTML電子郵件部分,它用於html電子郵件正文。

1.1是TEXT/PLAIN - 純文本電子郵件正文 - 純文本消息,您將需要使用這一個。

+0

嗨,我使用的第三個參數值爲1.2,但與此值我得到空身體內容。你能分享爲什麼我得到空的身體內容的解決方案嗎? – user2393886

+0

是否有html body需要獲取?也許電子郵件只是純文本 – c2h5oh

+0

是的,正文內容是純文本,可能是html。這將取決於repliers。在搜索時,1.2用於這兩種類型的內容。但它實際上不起作用。 – user2393886

相關問題