Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I would like to send an email using Gmail SMTP server through PHP Mailer.

this is my code

<?php
require_once('class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = '[email protected]';
$mail->Password = 'valid password';
$mail->SMTPAuth = true;

$mail->From = '[email protected]';
$mail->FromName = 'Mohammad Masoudian';
$mail->AddAddress('[email protected]');
$mail->AddReplyTo('[email protected]', 'Information');

$mail->IsHTML(true);
$mail->Subject    = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";
$mail->Body    = "Hello";

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

but I receive this following error

Mailer Error: SMTP Error: The following recipients failed: [email protected]

SMTP server error: SMTP AUTH is required for message submission on port 587

my domain is vatandesign.ir

like image 815
Mohammad Masoudian Avatar asked Apr 16 '13 22:04

Mohammad Masoudian


People also ask

What is SMTP in PHPMailer?

PHPMailer is the classic email sending library for PHP. It supports several ways of sending email messages such as mail(), Sendmail, qmail, and direct dispatch to SMTP servers. In addition, it provides a list of advanced features: SMTP authentication. secure/MIME encryption.

How do you fix SMTP error could not connect to SMTP host?

There are many popular cases for the failure of SMTP connection in PHPMailer and lack of SSL is one of that too. There might be a case, that the Open SSL extension is not enabled in your php. ini which is creating the connection problem. So, once you enable the extension=php_openssl.


4 Answers

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "password";
$mail->SetFrom("[email protected]");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("[email protected]");

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

This code above has been tested and worked for me.

It could be that you needed $mail->SMTPSecure = 'ssl';

Also make sure you don't have two step verification switched on for that account as that can cause problems also.

UPDATED

You could try changing $mail->SMTP to:

$mail->SMTPSecure = 'tls';

It's worth noting that some SMTP servers block connections. Some SMTP servers don't support SSL (or TLS) connections.

like image 90
andrew-caulfield Avatar answered Oct 17 '22 00:10

andrew-caulfield


So I just solved my own "SMTP connection failure" error and I wanted to post the solution just in case it helps anyone else.

I used the EXACT code given in the PHPMailer example gmail.phps file. It worked simply while I was using MAMP and then I got the SMTP connection error once I moved it on to my personal server.

All of the Stack Overflow answers I read, and all of the troubleshooting documentation from PHPMailer said that it wasn't an issue with PHPMailer. That it was a settings issue on the server side. I tried different ports (587, 465, 25), I tried 'SSL' and 'TLS' encryption. I checked that openssl was enabled in my php.ini file. I checked that there wasn't a firewall issue. Everything checked out, and still nothing.

The solution was that I had to remove this line:

$mail->isSMTP();

Now it all works. I don't know why, but it works. The rest of my code is copied and pasted from the PHPMailer example file.

like image 24
Evan Butler Avatar answered Oct 17 '22 00:10

Evan Butler


Also worth noting that if you have two factor authentication enabled, you'll need to setup an application specific password to use in place of your email account's password.

You can generate an application specific password by following these instructions: https://support.google.com/accounts/answer/185833

Then set $mail->Password to your application specific password.

like image 8
Tim Carr Avatar answered Oct 17 '22 02:10

Tim Carr


It seems that your server fails to establish a connection to Gmail SMTP server. Here are some hints to troubleshoot this: 1) check if SSL correctly configured on your PHP (module that handle it isn't installed by default on PHP. You have to check your configuration in phph.ini). 2) check if your firewall let outgoing calls to the required port (here 465 or 587). Use telnet to do so. If the port isn't opened, you'll then require some support from sysdmin to setup the config. I hope you'll sort this out quickly!

like image 5
Pr Shadoko Avatar answered Oct 17 '22 01:10

Pr Shadoko