无法向IMAP服务器进行身份验证:[AUTHENTICATIONFAILED]无效的凭据(失败)

问题描述 投票:1回答:1

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

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

麻烦在哪里?

PHP代码:

<?php   
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = '[email protected]';
$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);
php imap
1个回答
0
投票

[如果您使用的是G Suite,则必须与您的G Suite管理员联系,以在Google管理控制台中启用对用户应用的不安全访问,URL如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分钟内生效,请等待一段时间)
© www.soinside.com 2019 - 2024. All rights reserved.