2016-11-17 45 views
-2

我嘗試使用下面的代碼來讀取Gmail收件箱:導致「登錄失敗次數過多」的錯誤讀的Gmail收件箱

set_time_limit(4000); 
    // Connect to gmail 
    $imapPath = '{imap.gmail.com:993/imap/ssl}INBOX'; 
    $username = '[email protected]'; 
    $password = 'password'; 
    // try to connect 
    $inbox = imap_open($imapPath,$username,$password,NULL,1) or die('Cannot connect to Gmail: ' . print_r(imap_errors())); 
    $emails = imap_search($inbox,'UNSEEN'); 
    $output = ''; 
    foreach($emails as $mail) { 
     $headerInfo = imap_headerinfo($inbox,$mail); 
     $output .= $headerInfo->subject.'<br/>'; 
     $output .= $headerInfo->toaddress.'<br/>'; 
     $output .= $headerInfo->date.'<br/>'; 
     $output .= $headerInfo->fromaddress.'<br/>'; 
     $output .= $headerInfo->reply_toaddress.'<br/>'; 
     $emailStructure = imap_fetchstructure($inbox,$mail); 
     if(!isset($emailStructure->parts)) { 
      $output .= imap_body($inbox, $mail, FT_PEEK); 
     } else { 
      //  
     } 
     echo $output; 
     $output = ''; 
    } 
    // colse the connection 
    imap_expunge($inbox); 
    imap_close($inbox); 

但我收到以下錯誤:

Array ( [0] => [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) [1] => Too many login failures)

+1

查看您的錯誤URL並閱讀它。可能是您發送的密碼錯誤,或者會在帳戶中啓用「兩步驗證」功能。 – Tiger

+0

https://davidwalsh.name/gmail-php-imap或https://arjunphp.com/reading-emails-from-gmail-using-php-imap/ –

+0

我試過這些代碼,但仍然是相同的錯誤.. –

回答

0

明白了通過在我的Gmail設置中允許訪問「如果您使用新位置或設備登錄」

0

更改Gmail記帳設置: -

啓用IMAP。

狀態:啓用IMAP enter image description here