阅读Gmail收件箱会导致登录失败次数太多'错误

时间:2016-11-17 06:51:44

标签: php gmail imap

我正在尝试使用以下代码阅读gmail收件箱:

    set_time_limit(4000);
    // Connect to gmail
    $imapPath = '{imap.gmail.com:993/imap/ssl}INBOX';
    $username = 'usenamer@gmail.com';
    $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);

但是我收到以下错误:

  

阵   (       [0] =&gt; [ALERT]请通过网络浏览器登录:https://support.google.com/mail/accounts/answer/78754(失败)[1] =&gt;登录失败次数太多)

3 个答案:

答案 0 :(得分:0)

通过允许访问&#34;如果您从新位置或设备登录而修复此问题&#34;在我的Gmail设置中

答案 1 :(得分:0)

更改Gmail会计设置: -

启用IMAP。

状态:已启用IMAP enter image description here

答案 2 :(得分:0)

开启安全性较低的应用here

Allow less secure apps: If you don't use 2-Step Verification, you might need to allow less secure apps to access your account.

参考:support.google.com