How to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP?

Spread the love

Sometimes, we want to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP.

In this article, we’ll look at how to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP.

How to fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP?

To fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP, we set the SMTPSecure property to 'tls'.

For instance, we write

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "email@gmail.com";
$mail->Password = "password";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

if (!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

to create the PHPMailer $mail object.

We set SMTPSecure to send emails over TLS.

Then we set IsHTML to true to send HTML email.

And then we set the Username and Password to send emails with the HTML server.

After that we set the from, subject, body, and to address with

$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("email@gmail.com");

Finally, we send the email with Send.

Conclusion

To fix ‘Unable to send email using Gmail SMTP server through PHPMailer, getting error: SMTP AUTH is required for message submission on port 587’ error with PHP, we set the SMTPSecure property to 'tls'.

Leave a Reply

Your email address will not be published. Required fields are marked *