我正在编写可以从服务器获取电子邮件的代码(目前我在gmail服务器上进行测试)。当我尝试检索特定的电子邮件时,我尝试在没有回复的情况下获取新邮件,但它会检索回复邮件而不是父邮件。
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox';
$username = 'abc@gmail.com';
$password = 'password';
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
$emails = imap_search($inbox,'FROM abc@gmail.com');
if($emails) {
$output = '';
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = quoted_printable_decode(imap_fetchbody($inbox,$email_number,1));
//$message = imap_fetchbody($inbox,$email_number,2);
//$structure = imap_fetchstructure($inbox, $email_number);
$email = $overview[0]->from;
$subject = $overview[0]->subject;
$date = $overview[0]->date;
$output.= '<div class="body">'.$message.'</div>';
}
echo $output;
}
imap_close($inbox);
?>