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

 In All Tutorials
Last Updated On August 09, 2019

186 Upvotes | 14 comments

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. In such a case, I would suggest 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 pepipost.com 25
    

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

    Trying 202.162.247.93...
    Connected to pepipost.com.
    Escape character is '^]'.
    220 pepipost.com ESMTP Postfix
    

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

    Trying 202.162.247.93...
    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('smtp.pepipost.com');

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.

Recent Posts
Showing 8 comments
  • Tino Qu
    Reply

    Good reference! Just to add, I am using Postfix and had my LetsEncrypt certs set wrong in within the Postfix settings. It must have fullchain.pem and privkey.pem and then it worked.

  • Shakti
    Reply

    I had a similar issue. I had installed an old PHPMailer version 1.72 which is incompatible to manage SSL connections. I updated the version and solved it as recommended by you. Thank you so much!

  • jenn
    Reply

    PHPMailer 1.72 :flushed: Isn’t that too old?

  • Fleur H
    Reply

    This might sound strange but I faced a similar issue and I received the most unlikedly solution!

    Had been Using 7.x-1.7 and the module started giving the error that it could not connect to the host.

    I have it pointed at Amazon SES. Disabling and re-enabling the module fixed the problem in my case.

  • smiti0628
    Reply

    Anyone having a experience of how to deal with multi-threading in PHPMailer, so that i can send more emails in one connection?

  • Romit
    Reply

    In my case, it was the SSL problem. I enabled the extension=php_openssl.dll in the ini file and it worked like a charm.

  • Dinesh Singh
    Reply

    Many people might find a “Register Mail ids” option in the Cpanel. If you add your email address there, in around 30 mins it should work just fine with a simple php mail function.

  • David Gill
    Reply

    Nice documentation of SMTP error. Thanks Dibya, for sharing.
    It seems the outPorts plugin is no more working and is depreciated. You can have a look.

Leave a Comment

We are always looking for fresh talent to contribute to our blog. If you have a knack for writing and a passion for email marketing, we would love to have your post on our Guest Blog .

Submit a Guest Post
Pepi thinking

Start typing and press Enter to search

Thank you for your details!

Fill out your information below, and we will send you a PepiAlert, that will describe your domain’s email deliverability situation. Please note that your email address must match the domain, or the domain must be owned by the company matching the email address. We have the right to refuse the request, if we can’t verify the information.

*All fields are required



Pin It on Pinterest