Can not authenticate to IMAP server: [AUTHENTICATIONFAILED] Invalid credentials (Failure)

孤人 提交于 2020-03-03 12:42:05

问题


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)

  1. https://admin.google.com
  2. Security settings
  3. Basic settings (first link)
  4. Less secure apps -> Go to settings for less secure apps
  5. Enforce access to less secure apps for all users (Not Recommended)
  6. Save
  7. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!