【问题标题】:Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)无法对 IMAP 服务器进行身份验证:[AUTHENTICATIONFAILED] 凭据无效(失败)
【发布时间】:2018-12-21 21:31:30
【问题描述】:

当我尝试通过 imap 从电子邮件中获取消息时 - 我有这个错误:

无法对 IMAP 服务器进行身份验证:[AUTHENTICATIONFAILED] 凭据无效(失败) 操作系统:debian 7

麻烦在哪里?

PHP 代码:

<?php   
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = 'email@gmail.com';
$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);

【问题讨论】:

  • 该信息似乎非常清晰简洁。您提供的凭据不正确。
  • 我输入了有效的电子邮件和密码
  • 显然你没有。除了他们需要的用户/通行证之外,还有其他 vars 吗?
  • 我认为,没有
  • 这是全部信息吗? Gmail 有一系列安全机制来防止帐户在“不寻常的地方”被使用,并且会出于多种原因阻止您。通常链接有错误,您可以生成应用专用密码,或实施 OAuth。

标签: php imap


【解决方案1】:

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

或走这条路(对于 G Suite 管理员)

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

【讨论】:

  • 为这两个帐户打开不太安全的应用程序设置。我遇到了同样的问题。首先,我从管理员帐户启用了不太安全的应用程序。然后我启用了用户的电子邮件。终于成功了
【解决方案2】:

如果您未使用 G Suite,请查看以下网址并开启“不太安全的应用” https://myaccount.google.com/lesssecureapps

【讨论】:

    猜你喜欢
    • 2014-10-15
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2015-02-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多