【发布时间】: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。