View Categories

[Fixed] PHPMailer SMTP Error: Could not connect to SMTP host

10 min read

Introduction #

Facing an error which says “PHPMailer SMTP Error: Could not connect to SMTP host”?

Let’s solve it together.

PHPMailer is one of the most popular open-source written in PHP for sending emails. While it’s easy to deploy and start sending emails, but there is a common error which most of us might be facing.

In this document, I have tried sharing the answer for some of the most occurring errors with the PHPMailer:

#Error: PHPMailer: SMTP Error: Could Not Connect To SMTP Host #

Depending on your situation, there can be multiple reasons for the occurrence of this error. So, please try to go through the different scenarios below and pick the one which is closest to your use case.

 

Possible Problem 1: Problem With The Latest Version Of PHP #

I tried using PHPMailer in many projects in the past and it worked buttery smooth. But, when I updated the PHP version to 5.6, I started getting an SMTP connection error. Later, I observed that this problem is there with the latest version of the PHP.

I noticed that in the newer version, PHP has implemented stricter SSL behaviour which has caused this problem.

Here is a help doc on PHPMailer wiki which has a section around this.

And, here is the quick workaround mentioned in the above wiki, which will help you fix this problem:

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

You can also change these settings globally, in the php.ini file but that’s a really bad idea because PHP has done these SSL level strictness for very good reasons only.

This solution should work fine with PHPMailer v5.2.10 and higher.

 

Possible Problem 2: Using Godaddy As The Hosting Provider #

If you are running your code on Godaddy and trying to connect to some third-party SMTP provider like smtp.pepipost.com or smtp.sendgrid.com and getting some errors like this;

Mailer Error: SMTP connect() failed.

then nothing to really debug further, because it is because of a wried rule imposed by Godaddy on its user, where Godaddy has explicitly blocked the outgoing SMTP connection to ports 25, 587 and 465 to all external servers except for their own. Godaddy primarily wants their users to use their own SMTP instead of any third party SMTP, which is not at all an acceptable move for the developer community and many have has expressed their frustration in form of issues on StackOverflow too.

Your PHPmailer code might work perfectly fine on a local machine but the same code, when deployed on Godaddy server might not work and that’s all because of this silly rule implemented by Godaddy.

Here are few workarounds to avoid SMTP connection issues in Godaddy:

#1- Use Godaddy SMTP Instead Of Any Third Party: #

In case you are sending 1-1 personalized emails, then using Godaddy SMTP makes sense. For that, just make the following changes in your PHPMailer code and you will be done;

$mail->isSMTP();
$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false; 
$mail->Port = 25;

Note: Godaddy also restricts using any free domains like gmail, yahoo, hotmail, outlook, live, aim or msn as sender domain/From address. This is mostly because these domains have their own SPF and DKIM policies and some one can really forg the from address if allowed without having custom SPF and DKIM.

But, in case you want to send bulk/emails at scale then it becomes a bottleneck with high chances of your emails been landed in spam and your domain/IP address getting blacklisted. In such a case, I would suggest checking your email blacklist status and going with an option no #2.

#2- Use Email APIs Instead Of Any SMTP: #

Godaddy can block the outgoing SMTP ports but can’t really block the outgoing HTTP ports (80, 8080) ???? So, I would recommend using some good third party email service provider who provides email APIs to send emails. Most of these providers have code libraries/SDKs like PHPMailer which you can install and include in your code to start sending emails. Unlike using Godaddy’s local SMTP, using email APIs will give you a better control on your email deliverability.

 

Possible Problem 3: Getting SMTP Connection Failure On A Shared Hosting Provider #

If you are running your code on a shared hosting provider and trying to connect to some third-party SMTP provider like smtp.pepipost.com or smtp.sendgrid.com and getting some errors like this;

SMTP connect() failed.

then, this is mostly because of the firewall rules on their infrastructure which explicitly blocks the outgoing SMTP connection to ports 25, 587 and 465 to all external servers. This rule is primarily to protect the infrastructure from sending spam, but also a really frustrating situation for developers like us.

The only solution to this is, same as I suggested above in the Godaddy section (Use Email APIs instead of any SMTP) or contact the hosting provider to allow connection to SMTP ports.

How to check whether your outgoing port (25, 587 or 465) is really blocked or not?

1. Trying doing telnet
Using telnet command you can actually test whether the port is opened or not.

//Type the following command to see if Port 25 is blocked on your network. 
telnet migomta.one 25

If Port 25 is not blocked, you will get a successful 220 response (text may vary).

Trying 103.168.217.2... 
Connected to migomta. 
Escape character is '^]'. 
220 migomta.one ESMTP Migomta

If Port 25 is blocked, you will get a connection error or no response at all.

Trying 103.168.217.2...
telnet: connect to address 202.162.247.93: Connection refused
telnet: Unable to connect to remote host

 

2. Use outPorts
outPorts is a very good open-source on GitHub to which scans all your ports and gives the result.
Once outPorts is installed, you can type the following command in the terminal to check port 25 connectivity:
outPorts 25

 

Possible Problem 4: SELinux Blocking Issue #

In case you are some error like the following:

SMTP -> ERROR: Failed to connect to server: Permission denied (13)

then, the most probably your SELinux is preventing PHP or the webserver from sending emails.

This problem is mostly with Linux based machines like RedHat, Fedora, Centos, etc.

How to debug whether it’s really the SELinux issue which is blocking these SMTP connections?

You can use the getsebool command to check whether the httpd daemon is allowed to make an SMTP connection over the network to send an email.

getsebool httpd_can_sendmail
getsebool httpd_can_network_connect

This command will return a boolean on or off. If it’s disabled, then you will see an output like this;

getsebool: SELinux is disabled

We can turn it on using the following command:

sudo setsebool -P httpd_can_sendmail 1
sudo setsebool -P httpd_can_network_connect 1

If you are running your code on a shared hosting provider and trying to connect to some third-party SMTP provider like smtp.pepipost.com or smtp.sendgrid.com and getting some errors like this.

 

Possible Problem 5: PHPMailer SMTP Connection Failed Because Of SSL Support Issue With PHP #

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.dll in the ini file.

Enable debug output, so that you can really see that SSL is the actual problem or not. PHPMailer gives a functionality by which you can get detailed logs of the SMTP connection.

You can enable this functionality by including the following code in your script;

$mail->SMTPDebug = 2;

By setting the value of SMTPDebug property to 2, you will be actually getting both server and client level transcripts.

For more details on the other parameter values, please refer the official PHPMailer Wiki.

In case you are using Godaddy hosting, then just enabling SSL might not fix your problem. Because there are other serious challenges with Godaddy which you can refer in the above godaddy section.

 

Possible Problem 6: PHPMailer Unable To Connect To SMTP Because Of The IPv6 Blocking Issue #

There are some set of newer hosting companies which includes DigitalOcean provides IPv6 connectivity but explicitly blocks outgoing SMTP connections over IPv6 but allow the same over IPv4.

While this is not a major issue, because this can be workaround by setting the host property to an IPv4 address using the gethostbyname function.

$mail->Host = gethostbyname('sn1.migomta.one');

Note: In this approach, you might face a certificate name check issue but that can be workaround by disabling the check, in SMTPOptions.
But, this is mostly an extreme case, most of the times it’s the port block issue by the provider, like DigitalOcean in this case.
So, it is important to first get confirmed whether the port is really unlocked or not, before digging further into the solution.

 

Possible Problem 7: Getting The Error “Could Not Instantiate Mail Function” #

This issue happens primarily when your PHP installation is not configured correctly to call the mail() function. In this case, it is important to check the sendmail_path in your php.ini file. Ideally, your sendmail_path should point to the sendmail binary (usually the default path is /usr/sbin/sendmail).

Note: In case of Ubuntu/Debian OS, you might be having multiple .ini files (under the path /etc/php5/mods-available), so please ensure that you are making the changes at all the appropriate places.

If this configuration problem is not the case, then try further debugging and check whether you have a local mail server installed and configured properly or not. You can install any good mail server like Postfix.

Note: In case all of the above things are properly in place and you’re still getting this error of “Could not instantiate mail function”, then try to see if you are getting more details of the error. If you see some message like “More than one from person” in the error message then it means that in php.ini the sendmail_path property already contains a from -f parameter and your code is also trying to add a second envelope from, which is actually not allowed.

 

What Is The Use Of IsSMTP()? #

isSMTP() is been used when you want to tell PHPMailer class to use the custom SMTP configuration defined instead of the local mail server.

Here is a code snippet of how it looks like;

require 'class.phpmailer.php'; // path to the PHPMailer class
       require 'class.smtp.php';
           $mail = new PHPMailer();
           $mail->IsSMTP();  // telling the class to use SMTP
           $mail->SMTPDebug = 2;
           $mail->Mailer = "smtp";
           $mail->Host = "ssl://smtp.gmail.com";
           $mail->Port = 587;
           $mail->SMTPAuth = true; // turn on SMTP authentication
           $mail->Username = "myemail@example.com"; // SMTP username
           $mail->Password = "mypasswword"; // SMTP password
           $Mail->Priority = 1;
           $mail->AddAddress("myemail@gmail.com","Name");
           $mail->SetFrom($visitor_email, $name);
           $mail->AddReplyTo($visitor_email,$name);
           $mail->Subject  = "This is a Test Message";
           $mail->Body     = $user_message;
           $mail->WordWrap = 50;
           if(!$mail->Send()) {
           echo 'Message was not sent.';
           echo 'Mailer error: ' . $mail->ErrorInfo;
           } else {
           echo 'Message has been sent.';
           }

Many times developers get the below error:

"SMTP -> ERROR: Failed to connect to server: Connection timed out (110). SMTP Connect() failed. Message was not sent. Mailer error: SMTP Connect() failed."

If you’re constantly getting the above error message, then just try identifying the problem as stated in the above sections.

Powered by BetterDocs

Leave a Reply

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

Privacy Settings
We use cookies to enhance your experience while using our website. If you are using our Services via a browser you can restrict, block or remove cookies through your web browser settings. We also use content and scripts from third parties that may use tracking technologies. You can selectively provide your consent below to allow such third party embeds. For complete information about the cookies we use, data we collect and how we process them, please check our Privacy Policy
Youtube
Consent to display content from Youtube
Vimeo
Consent to display content from Vimeo
Google Maps
Consent to display content from Google
Spotify
Consent to display content from Spotify
Sound Cloud
Consent to display content from Sound