php - 无法向 IMAP 服务器进行身份验证 : [AUTHENTICATIONFAILED] Invalid credentials (Failure)

标签 php imap

当我尝试通过 imap 从电子邮件获取消息时 - 我遇到此错误:

Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure) OS: debian 7

问题出在哪里?

PHP 代码:

<?php   
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e2b232f27220e29232f2722602d2123" rel="noreferrer noopener nofollow">[email protected]</a>';
$password = 'pass';

/* try to connect */
$inbox = imap_open($hostname,$username,$password, NULL, 1, array('DISABLE_AUTHENTICATOR' => 'GSSAPI')) or die('Cannot connect to Gmail: ' . imap_last_error());

/* 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) {

    /* 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);

最佳答案

如果您使用的是 G Suite,则必须联系 G Suite 管理员,以便在 Google 管理控制台中为用户的应用启用不安全的访问权限,网址如 https://admin.google.com/{YOUR_DOMAIN_NAME}/AdminHome?hl=en#ServiceSettings/notab=1&service=securitysetting&subtab=lesssecureappsaccess

或按此方式(G Suite 管理员)

  1. https://admin.google.com
  2. 安全设置
  3. 基本设置(第一个链接)
  4. 安全性较低的应用 -> 前往安全性较低的应用的设置
  5. 强制所有用户访问安全性较低的应用(不推荐)
  6. 保存
  7. 利润(1-2分钟后生效,请稍等)

关于php - 无法向 IMAP 服务器进行身份验证 : [AUTHENTICATIONFAILED] Invalid credentials (Failure),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326173/

相关文章:

php - 如何在 PHP 中使用 IMAP 获取邮件正文内容?

php - 通过 IMAP 删除 GMail 聊天记录

PHPUnit代码覆盖生成导致内存耗尽

php - 最好的 PHP 编程方法?

php - 使用 PHP 解析 .htaccess 文件

gmail - IMAP 客户端如何以编程方式取消 Gmail 标签重命名

delphi - Indy IMAP 在点处切断消息

java - 从 PHP 调用 Java 程序

php - 如何在 PHP 网络服务中获取客户端的 IP 地址?

linux - 无法远程通过telnet连接到imap