Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPMailer: SMTP Error: Could not connect to SMTP host

I've used PHPMailer on several projects but now I'm stuck. It gives me the error:
SMTP Error: Could not connect to SMTP host.
I've tried sending email from Thunderbird and it works ! But not through PHPMailer ... Here are the settings from Thunderbird:

Server name: mail.exampleserver.com
Port: 587
Username: [email protected]
Secure Authentication: No
Connection Security: STARTTLS

I've compared these with the server at my last project where I used PHPMailer and they were:

Server name: mail.exampleserver2.com
Port: 465
Username: [email protected]
Secure Authentication: No
Connection Security: SSL/TLS

My php code is:

 $mail = new PHPMailer();  $mail->IsSMTP(); // send via SMTP  $mail->Host = SMTP_HOST; // SMTP servers  $mail->Port = SMTP_PORT; // SMTP servers  $mail->SMTPAuth = true; // turn on SMTP authentication  $mail->Username = SMTP_USER; // SMTP username  $mail->Password = SMTP_PASSWORD; // SMTP password  $mail->From = MAIL_SYSTEM;  $mail->FromName = MAIL_SYSTEM_NAME;  $mail->AddAddress($aSecuredGetRequest['email']);  $mail->IsHTML(true); // send as HTML 

Where I am wrong?

like image 551
Ilian Andreev Avatar asked Aug 13 '10 14:08

Ilian Andreev


People also ask

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.

What does SMTP error could not connect to SMTP host mean?

In such scenarios, the mailing software will not be able to contact the SMTP server, causing connection errors. If the mail server has another service running on the SMTP port configured, the same issue occurs. Another issue related to mail server is the lack of support for encryption.


1 Answers

Since this questions shows up high in google, I'd like to share here my solution for the case where PHP was just upgraded to version 5.6 (which has stricter SSL behavior).

The PHPMailer wiki has a section on this:

https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#php-56-certificate-verification-failure

The suggested workaround is including the following piece of code:

$mail->SMTPOptions = array(     'ssl' => array(         'verify_peer' => false,         'verify_peer_name' => false,         'allow_self_signed' => true     ) ); 

This should work for PHPMailer 5.2.10 (and up).

Note: Obviously, and also as suggested in that wiki, this should be a temporary solution!

The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one.

like image 131
Marten Koetsier Avatar answered Oct 08 '22 12:10

Marten Koetsier