2011-03-03 6 views
6

이메일 본문 내용을 가져올 수 없습니다. 내가 Gmail의 이메일을 가져 오기 위해 사용하는 경우 PHP에서 IMAP를 사용하여 메일 본문 내용을 가져 오는 방법은 무엇입니까?

는 이메일 본문의 내용이 표시됩니다 내 코드

<?php 
/* connect to server */ 
$hostname = '{myserver/pop3/novalidate-cert}INBOX'; 
$username = 'username'; 
$password = 'password'; 

/* try to connect */ 
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Tiriyo: ' . imap_last_error()); 
//echo $inbox; 
/* 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) { 
    //$email_number=$emails[0]; 
//print_r($emails); 
    /* 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); 
?> 

,하지만 난 내 메일 서버를 사용하면, 나는 이메일의 본문 내용을 가져올 수 없습니다.

이 문제를 해결할 수 있습니까?

+0

어떤 메일 서버를 사용하고 있습니까? – Ben

+0

테스트 됨. 당신의 스크립트는 Gmail에서 잘 작동하고 있습니다. 오류는 아마도 메일 서버에서오고 있습니다. 당신이 제공 할 수있는 다른 세부 사항이 있습니까? – Ben

+0

아마 http://stackoverflow.com/questions/5170489/php5-imap-i-aint-got-no-body – dkarp

답변

38

것은 내가 오류이 줄 이제

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

함께, 해결책을 발견했다, 내가 가진 다음

텍스트/HTML 형식의 본문 내용을 받으려면

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

을 사용하고 있습니다 사용 가능한 옵션에 대한 데이터를 제공합니다. 이것은 하나 일 수도 있습니다

()Root Message Part (multipart/related) 
(1) The text parts of the message (multipart/alternative) 
(1.1) Plain text version (text/plain) 
(1.2) HTML version (text/html) 
(2) The background stationary (image/gif) 
+0

감사합니다. 정확히 똑같은 것을 검색해 왔습니다 ... 그래서 jaja ...를 확인하는 것이 좋습니다. 인터넷에서 예제를 복사하여 붙여 넣기 만하면됩니다. –

+0

lookup fetch_structure, u 인코딩 된 메시지를 디코딩해야합니다. – Andrew

0

제타 메일 구성 요소는 훨씬 더 convenient fetching of mails from IMAPPOP을 허용하며 수신 이메일을 멋지고 깨끗한 객체 구조로 구문 분석하여 쉽게 처리 할 수 ​​있습니다.

+1

그는 IMAP이 아닌 POP를 사용하고 있습니다. 'imap_open' 호출의 첫 번째 매개 변수를 확인하십시오. – dkarp

+0

죄송합니다, 잘못 알아 들었습니다. 그러나 POP에서도 작동합니다. http://incubator.apache.org/zetacomponents/documentation/trunk/Mail/tutorial.html#retrieving-mail-using-pop3 – tobyS

+0

IMAP과 POP를 모두 반영 할 수 있도록 내 답변이 업데이트되었습니다. . – tobyS

관련 문제