Snyzzle
Snyzzle

Reputation: 1

PHPMailer sends email with SMTP authentication but Gmail still claims it cannot validate sender

I am using PHPMailer to send SMTP authenticated emails from my PHP scripts. The PHPMailer script works, it throws no errors and I receive my test emails, however they are still flagged as not authenticated in my gmail inbox. (Shown with the little question mark icon).

This is my code:

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

require './PHPMailer/src/Exception.php';
require './PHPMailer/src/PHPMailer.php';
require './PHPMailer/src/SMTP.php';


$mail = new PHPMailer(TRUE);

try {
    $mail->setFrom('[email protected]', 'My Domain');
    $mail->addAddress('[email protected]', 'Me');
    $mail->Subject = 'PHPMailer Test Message';
    $mail->isHTML(TRUE);
    $mail->Body = 'Test email';

    /*SMTP settings*/
    $mail->isSMTP();
    $mail->Host = 'smtp.ionos.co.uk';
    $mail->SMTPAuth = TRUE;
    $mail->SMTPSecure = 'tls';
    $mail->Username = '[email protected]';
    $mail->Password = 'myPassword';
    $mail->Port = 587;


    if(!$mail->send()) {
        echo 'Message was not sent.';
        echo 'Mailer error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }

}
catch (Exception $e)
{
    echo $e->errorMessage();
}
catch (\Exception $e)
{
    echo $e->getMessage();
}
?>

Does anyone know why this email is still flagged? I am using PHPMailer 6.1, the server is running PHP 7.4

Upvotes: 0

Views: 1335

Answers (2)

Snyzzle
Snyzzle

Reputation: 1

So responding to the comments, I solved it by adding an SPF (TXT) Record to my domain's DNS. The host field was set to @ and the weighting to Hardfail

Thanks for the help all.

Upvotes: 0

Stef
Stef

Reputation: 171

You have to allow gmail to validate your emails with an spf entry in your DNS settings. For more information, please refer to Google docs : https://support.google.com/a/answer/33786

Upvotes: 1

Related Questions