PHPMailer

I have been using PHPMailer for sending mails and it used to work before. Since last few days I am getting the following error. Nothing has changed except maybe the new PHP version installed by the hosting company.

SMTP Error: Could not connect to SMTP host. tls://smtp.gmail.com:587

Any ideas?

Thanks.

1 Like

Try to change tls with ssl and 587 with 465 to connect to SMTP.

I did that too but getting the same error.

Could you try running with SMTP debugging enabled to see if you can get some more information about why PHPMailer couldn’t connect to the SMTP host?

1 Like

It is not giving specific error message, else I would have an idea what went wrong.

This is the message I am getting when debugging:

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

This is the code I am using:

function smtpmailer($to, $from, $from_name, $subject, $body)
{
global $error;
$mail = new PHPMailer\PHPMailer\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 = ‘tls’; // secure transfer enabled REQUIRED for GMail
$mail->Host = ‘tls://smtp.gmail.com:587’; //‘smtp.gmail.com’;
#echo "Mail server host: " . $mail->Host;
$mail->Port = 587;
$mail->Username = “my gmail id”;
$mail->Password = “my password”;
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send()) 
{
    $error = 'Mail error in smtpmailer with gethost: '.$mail->ErrorInfo; 
    echo $error;
    return false;
} 
else 
{
    $error = 'Message sent!';
    return true;
}

}

I use Google SMTP and always use SSL encryption under port 465.

It’s working fine, even in the newly updated version of PHP.

Forgive me for English using translator

I just replaced the encryption part and port from tls to ssl and from 587 to 465, but still getting the same error.

Can you send me your code so that I will try using it and see if it works?

I will correct the code for you:

function smtpmailer($to, $from, $from_name, $subject, $body)
{
global $error;
$mail = new PHPMailer\PHPMailer\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 = "tls";
$mail->Host = "ssl://smtp.gmail.com:465";
//echo "Mail server host: " . $mail->Host;
//$mail->Port = 587;
$mail->Username = "your Gmail ID";
$mail->Password = "your password";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send()) 
{
    $error = 'Mail error in smtpmailer with gethost: '.$mail->ErrorInfo; 
    echo $error;
    return false;
} 
else 
{
    $error = 'Message sent!';
    return true;
}

}

making sure to replace “your Gmail ID” with your Gmail email and “your password” with your Google account password.

2 Likes

Sorry, still getting the same error.

Then:

function smtpmailer($to, $from, $from_name, $subject, $body)
{
global $error;
$mail = new PHPMailer\PHPMailer\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"; // SSL security method - works in some cases
$mail->Host = "smtp.gmail.com"; // With SMTPSecure and Port you must remove the protocol and port from the host
//echo "Mail server host: " . $mail->Host;
$mail->Port = 465; // SMTP SSL port
$mail->Username = "your Gmail ID";
$mail->Password = "your password";
$mail->SetFrom($from, $from_name);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AddAddress($to);

if(!$mail->Send()) 
{
    $error = 'Mail error in smtpmailer with gethost: '.$mail->ErrorInfo; 
    echo $error;
    return false;
} 
else 
{
    $error = 'Message sent!';
    return true;
}

}

After that, follow this link to let that work.

Nope, I guess I tried all possible ways and even used some of the examples found out there, but getting the same error. It was working fine some time back.

I edited the precedent post to include the CAPTCHA unlock link that lets the entire system work flawlessly. Please follow that link and the instructions they give to you.

Captcha is not enabled in my account, should I have to enable it?

No, you just need to follow the link, and you’re maybe set to go.

I followed the process. But still the same.

Did you enable Less Secure Apps in your Google Account? If not, do it.

Yes, I already did that last time and checked to make sure it is still enabled.

Anyone knows how to debug exactly what the reason for getting the error? I have tried with different gmail accounts and they all are giving error.

1 Like

i have the same issue as OP.
The only difference is that my SMTP Host is Yandex.

I also tried in every way, including configuring the way that it was, when it was working (like December 2018, the last time I have changed this project). But still SMTP is not connecting to Yandex host.

As a sidenote: I have Cloudflare as my DNS, although Cloudflare does not cache php files (so the problem is related to InfinityFree)

And I have no problem in my local machine.