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

Viewed 2388

I saw many similar questions, like my, but never the one, which helps me. I am trying PHPMailer from https://github.com/PHPMailer/PHPMailer. I was testing it with xampp server and everything worked fine. But when I uploaded whole phpMailer on my page, some issues appeared. In my log file, I am still getting this error:

PHP Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in /web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php:2089 Stack trace: #0 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1900): PHPMailer\PHPMailer\PHPMailer->smtpConnect() #1 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1614): PHPMailer\PHPMailer\PHPMailer->smtpSend() #2 web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php(1447): PHPMailer\PHPMailer\PHPMailer->postSend() #3 web/test/idk.php(31): PHPMailer\PHPMailer\PHPMailer->send() #4 {main} thrown in web/test/vendor/phpmailer/phpmailer/src/PHPMailer.php on line 2089`

Does anybody know, how to fix this? I am pretty sure my log in credentials are OK. The weird thing is that it is working on xampp fine, but not on my page. I am using the newest PHPMailer, and I didnt edited anything, so you can see the code on PHPMailer github. If someone needs additinal information, just let me know. Thank you so much!

EDIT: Here is my idk.php file (the code snippet didn't worked fine, but I think you can read it):

<?php 
declare( strict_types = 1 );

// Check if the form is submitted
if ( isset( $_POST['submit'] ) ) {
    // retrieve the form data by using the element's name attributes value as key
    
    $subject = $_REQUEST['subject'];
    $body = $_REQUEST['body'];
    $sender = $_REQUEST['sender'];

    require __DIR__ . '/vendor/autoload.php';

    $mailer = new \PHPMailer\PHPMailer\PHPMailer( true );
    //$mailer->SMTPDebug = \PHPMailer\PHPMailer\SMTP::DEBUG_SERVER tento řádek povoluje client -> server a server -> client zprávy

    $mailer->isSMTP();
    $mailer->Host = 'smtp.gmail.com';
    $mailer->SMTPAuth = true;
    $mailer->Username = 'my@mail.com';
    $mailer->Password = 'myPassword';
    $mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
    $mailer->Port = 587;
    $mailer->setFrom( 'my@mail.com' );
    $mailer->addReplyTo( 'my@mail.com' );
    $mailer->addAddress( $sender );
    $mailer->isHTML( true );
    $mailer->Subject = $subject;
    $mailer->Body = $body;
    $mailer->send();
}
?>

Maybe I should say that I am using .htaccess file on my web hosting. I am not sure, but maybe this is the reason, why I am getting those errors. When I am trying to access the idk.php file, I am getting http error 500. I did some research and there is some connection beetween this 500 error and htaccess file.

EDIT: I turned off less secure apps, but it still does not work. I changed port to 465 and now the error in on line 2076 in this file: https://github.com/PHPMailer/PHPMailer/blob/master/src/PHPMailer.php

3 Answers

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

I see you are using Google SMTP. Try enabling “less secure apps” in your Google account. Also make sure you have added your email address the “allowed senders” list.

As this was working on your local machine and not your server, you may also want to add your server IP to the allowed connections.

I’m not sure you need the leading backslash here:

$mailer = new \PHPMailer\PHPMailer\PHPMailer( true );

Or here:

$mailer->SMTPSecure = \PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
Related