2012-06-02 4 views
4

IMAP 기능을 사용하는 특정 메일 ID의 메일을 읽습니다. 그러나 plain text 메일에 대한 메일 내용을 읽을 수 없습니다. HTML Mails에 대해 완벽하게 작동합니다. 코드를 실행 한 후 모든 plain text 메일은 읽지 않은 채로 남아 있고 읽은 메일 메일과 제목과 같은 다른 메일은 읽을 수 있습니다. 콘텐츠를 읽는 데 문제가 있습니다. 여기에 내가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 본문을 가져올 수 있습니까? 어쩌면 전자 메일은 일반 텍스트 일뿐입니다 – c2h5oh

+0

예, 본문 내용은 일반 텍스트이며 html 일 수 있습니다. 그것은 repliers에 의존 할 것입니다. 검색시 두 유형의 콘텐츠 모두에 1.2가 사용됩니다. 그러나 실제적으로 작동하지 않습니다. – user2393886

관련 문제