问题
When i trying to get messages from email via imap - i have this error:
Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure) OS: debian 7
Where is trouble?
PHP code:
<?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);
回答1:
If you are using G Suite, you have to contact your G Suite administrator to enable not secure access for users apps in Google Admin panel, URL like https://admin.google.com/{YOUR_DOMAIN_NAME}/AdminHome?hl=en#ServiceSettings/notab=1&service=securitysetting&subtab=lesssecureappsaccess
or go this way (for G Suite admin)
- https://admin.google.com
- Security settings
- Basic settings (first link)
- Less secure apps -> Go to settings for less secure apps
- Enforce access to less secure apps for all users (Not Recommended)
- Save
- PROFIT (can take effect in 1-2 mins, wait for some time please)
来源:https://stackoverflow.com/questions/51326173/can-not-authenticate-to-imap-server-authenticationfailed-invalid-credentials