当我尝试通过 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 管理员)
- https://admin.google.com
- 安全设置
- 基本设置(第一个链接)
- 安全性较低的应用 -> 前往安全性较低的应用的设置
- 强制所有用户访问安全性较低的应用(不推荐)
- 保存
- 利润(1-2分钟后生效,请稍等)
关于php - 无法向 IMAP 服务器进行身份验证 : [AUTHENTICATIONFAILED] Invalid credentials (Failure),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51326173/