E
E
Evgeny Nikolaev2020-04-26 18:21:29
PHP
Evgeny Nikolaev, 2020-04-26 18:21:29

PHP. PhpMailer how to send SMTP mail via gmail.com from google? Why authorization error?

I'm trying to send mail via SMTP via Google Gmail.com using PhpMailer in PHP language. For some reason, an authorization error occurs. Although through Yandex, Mile and HotMail from Microsoft it is normally sent.

I am using the following code

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require "vendor/autoload.php";

$mail = new PHPMailer(true);

try {
  $mail->SMTPDebug = SMTP::DEBUG_SERVER;
  $mail->isSMTP();
  $mail->Host = "smtp.gmail.com";//Сервер SMTP gmail
  $mail->SMTPAuth = true;
  $mail->Username = "[email protected]";//В документации Google указано что логин это адрес вместе с собакой
  $mail->Password = "password";//Пароль
  $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;//Указываем что TLS
  $mail->Port = 587;
  $mail->setFrom("[email protected]","От кого отправляю");
  $mail->addAddress("[email protected]","");//Кому отправляем письмо
  $mail->isHTML(true);//Письмо в формате HTML
  $mail->Subject = "Тема сообщения";
  $mail->Body = "Содержание сообщения";
  $mail->AltBody = "Альтернативное содержание сообщения";

  $mail->send();
  echo 'Сообщение отправлено';
} catch (Exception $e) {
    echo "Ошибка отправки: {$mail->ErrorInfo}";
}


According to the information posted on the google website, SMTP should work exactly like this, here is the link https://support.google.com/mail/answer/7126229 while there are posts on the Internet that you need to use the OAuth 2 protocol (maybe this is not entirely relevant to this issue )

Just in case, I updated PHPMailer, the code that I have given above is the version downloaded with composer, although until recently I used a rather old PHPMailer library connected by require_once

It is strange that when debugging SMTP, the Gmail server swears at an incorrect username or password. But I double-checked they are correct. Maybe in Gmail you need to first enable support for sending via SMTP somewhere?

For those who, according to the article, will set up sending mail on Yandex, Mile or Microsoft servers: Microsoft (live.com and hotmail.com) have completely identical settings. For mail and Yandex, remove TLS, add ssl://smtp.next_server_address and port 465 in the host, otherwise the settings are the same.

Also, after turning on the switch for insecure applications at the link https://myaccount.google.com/lesssecureapps , nothing has changed, the authorization error is still there.

Here is PHPMailer debugger log:

2020-04-26 20:58:58 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP x21sm8657596ljm.74 - gsmtp
2020-04-26 20:58:58 CLIENT -> SERVER: EHLO [here server host]
2020-04-26 20:58:58 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [62.109.23.192]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 402PUTF250
SMT 26 20:58:58 CLIENT -> SERVER: STARTTLS
2020-04-26 20:58:58 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2020-04-26 20:58:58 CLIENT -> SERVER: EHLO [server host here]
2020-04-26 20:58:58 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [62.109.23.192]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN- CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2020-04-26 20:58:58 CLIENT -> SERVER: AUTH LOGIN
2020-04-26 20:58:58 SERVER -> CLIENT: 334 VXNlcm5hbU6
2020-04-26 20:58:58 CLIENT -> SERVER: [credentials hidden]
2020-04-26 20:58:58 SERVER -> CLIENT: 334
UGFzc3dvcmQ6 : [credentials hidden]
2020-04-26 20:58:59 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials x21sm8657596ljm.74 - gsmtp
2020-04-26 20:58:59 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials x21sm8657596ljm.74 - gsmtp
SMTP Error: Could not authenticate.
2020-04-26 20:58:59 CLIENT -> SERVER: QUIT
2020-04-26 20:58:59 SERVER -> CLIENT: 221 2.0.0 closing connection x21sm8657596ljm.74 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Nikolaev, 2020-04-27
@nikolaevevge

I did the following, the wisgest user suggested switching the flag of unsafe applications using the link https://myaccount.google.com/lesssecureapps, it was turned off for me, it didn’t help right away.
Then I removed the line from the code (commented it out): $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
added this $mail->SMTPSecure = 'tls';
Here is the final code with which I sent:

<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';
$mail = new PHPMailer(true);

try {
  $mail->SMTPDebug = SMTP::DEBUG_SERVER;
  $mail->isSMTP();
  $mail->Host = 'smtp.gmail.com';
  $mail->SMTPAuth   = true;
  $mail->Username   = '[email protected]';
  $mail->Password   = 'password';
  $mail->Port = 587;
  $mail->setFrom("[email protected]","Имя от кого отправлять");
  $mail->addAddress("[email protected]","");//Кому отправляем
//$mail->addReplyTo("[email protected]","Имя кому писать при ответе");
  $mail->SMTPSecure = 'tls';
  $mail->isHTML(true);//HTML формат
  $mail->Subject = "Тема сообщения";
  $mail->Body    = "Содержание сообщения";
  $mail->AltBody = "Альтернативное содержание сообщения";

  $mail->send();
  echo "Сообщение отправлено";
} catch (Exception $e) {
  echo "Ошибка отправки: {$mail->ErrorInfo}";
}

Already when everything worked, I saw that it turns out that google sent such a notification:
5ea5fd45bf5da648691348.jpeg
So perhaps the switch for unsafe applications mattered, despite the fact that I turned it off now and without it, it is also sent. There is also an assumption that after switching the settings of unsafe applications, this change is not applied immediately, so it makes sense to try again, for example, an hour after switching it.

D
Dimonchik, 2020-04-26
@dimonchik2013

2FA enabled?
checked with telnet? up to sending, not only authorization?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question