"Smtp Error: Could Not Authenticate" in PHPmailer

SMTP Error: Could not authenticate in PHPMailer

Try this instead :

$Correo->Username = "foo@gmail.com";

I tested it and its working perfectly without no other change

SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticate

The error message is very clear "Could not authenticate".
I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.

I suppose that in your question the fields username and password look like

$mail->Username = "@gmail.com";
$mail->Password = "";

just because you, obviously, don't want to share them, I would suggest to present them like this in the question

$mail->Username = "********@gmail.com";
$mail->Password = "********";

So if you are using the correct username and password there are other two things to check

  1. Maybe your setting "Access for less secure apps" is turned off.
    After you login from the web you can turn it on from this page
    https://www.google.com/settings/u/0/security/lesssecureapps

  2. If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.

Follow this link for a step by step guide on how to do it
http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm

SMTP: Could not authenticate with PHPMailer and XOauth2

I finally found the solution after 2 days. SMTP is only accepting full access "mail.google.com" for some reason. Any other scope given is rejected. After finding the solution I googled for why this is happening, and this guy explains a bit:

Using `gmail.send` scope with SMTP MSA

PHP Mailer returns PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate

This is gmail issue, you need Turn off "Less secure app access"

More info:
https://support.google.com/accounts/answer/6010255?hl=en#zippy=%2Cif-less-secure-app-access-is-on-for-your-account%2Cif-less-secure-app-access-is-off-for-your-account

Turn off "Less secure app access"

https://myaccount.google.com/lesssecureapps

Example (PHPMailer v6.2.0)

<?php

/**
* This example shows settings to use when sending via Google's Gmail servers.
* This uses traditional id & password authentication - look at the gmail_xoauth.phps
* example to see how to use XOAUTH2.
* The IMAP section shows how to save this message to the 'Sent Mail' folder using IMAP commands.
*/

//Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require 'vendor/autoload.php';

//Create a new PHPMailer instance
$mail = new PHPMailer();

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// SMTP::DEBUG_OFF = off (for production use)
// SMTP::DEBUG_CLIENT = client messages
// SMTP::DEBUG_SERVER = client and server messages
$mail->SMTPDebug = SMTP::DEBUG_SERVER;

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption mechanism to use - STARTTLS or SMTPS
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = 'CHANGEME';

//Password to use for SMTP authentication
$mail->Password = 'CHANGEME';

//Set who the message is to be sent from
$mail->setFrom('from@example.com', 'First Last');

//Set an alternative reply-to address
$mail->addReplyTo('replyto@example.com', 'First Last');

//Set who the message is to be sent to
$mail->addAddress('CHANGEME', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

$mail->msgHTML('This is a plain-text message body');


//Replace the plain text body with one created manually
$mail->AltBody = 'This is a plain-text message body';

//Attach an image file

//send the message, check for errors
if (!$mail->send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message sent!';
//Section 2: IMAP
//Uncomment these to save your message in the 'Sent Mail' folder.
#if (save_mail($mail)) {
# echo "Message saved!";
#}
}

Taken from https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps



Related Topics



Leave a reply



Submit