我想知道如何從Gmail帳戶取未讀電子郵件。我有一個Gmail帳戶,我希望來自Gmail帳戶的所有未讀電子郵件都通過PHP來到我的網站。我一直在執行各種代碼,但他們沒有幫助我。請幫我解決這個問題。想要從Gmail中取未讀電子郵件
我有一個代碼,我用來從gmail中獲取郵件,但是這些郵件並沒有被獲取。
<?php
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
echo $password = 'abcd';
/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
echo "got inbox";
/* grab emails */
$emails = imap_search($inbox);
/* if emails are returned, cycle through each... */
if($emails) {
echo "got 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);
/* 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>';
break;
}
echo $output;
}
/* close the connection */
imap_close($inbox);
?>
是否發生錯誤?如果是這樣,那是什麼? – Tim
提供有關您收到的錯誤的更多信息,以幫助人們給您更好的答案 –
我沒有收到任何錯誤,所以我很迷惑,我錯了。 – user1788261