Home » Php » php – Authenticate Email VIA SPF

php – Authenticate Email VIA SPF

Posted by: admin July 12, 2020 Leave a comment

Questions:

Okay, I cant figure this out. I am using PHPMailer to send an email from my domain – example.com to myself. I am sending the email to myself.(testing the google schema markup), but the emails are not authenticated.
Here is the code I use to send the email to myself.

$mail = new PHPMailer();
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'tls://smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';      
$mail->Username = '[email protected]';
$mail->Password = '';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');

$mail->Subject = 'Microdata Test';
$html = '
<html>
    <head>
        <script type="application/ld+json">
        {
            "@context":       "http://schema.org",
            "@type":          "EmailMessage",
            "description":    "Check this out",
            "potentialAction": {
                "@type": "ViewAction",
                "target":   "https://www.youtube.com/watch?v=eH8KwfdkSqU"
                }
        }
        </script>
    </head>
    <body>
    <p>
        This a test for a Go-To action in Gmail.
    </p>
    </body>
</html>
';

$mail->msgHTML($html);
$mail->send();

Here is my spf record:

v=spf1 a mx include:mailgun.org include:mydomain.com ~all

How to&Answers:

Since you are sending the email with Google’s smtp, you have to include it in your SPF:

include:_spf.google.com

Answer:

You are sending from your host to google over SMTP. This means that Google will see if you host is in your SPF record and act accordingly

So, you have to include your public ip in the SPF record and pray that this is enough.

Also, your username is myemail not [email protected]. And password should be the real password, of course.

If nothing works, consult PHPMailer logs, there should be more hints.