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

Published on 2019-07-17· Updated on 2024-04-10

296 Upvotes | 34 comments

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.


Are you facing an error that says, “PHPMailer SMTP Error: Could not connect to SMTP host?”

We’ve got you covered!

PHPMailer is one of the most popular open-source programs. While it's easy to deploy and get started with it, there can be a few common errors that most of us might face.

In this document, we will share the solutions for some commonly occurring errors with the PHPMailer:

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

Depending on the situation, there can be multiple reasons for this error. Review the scenarios given below and pick the one closest to your use case.


Possible Problem 1: Issues With The Latest Version Of PHP

When you update the new PHP version, you can get an SMTP connection error. 

The new version implements stricter SSL behavior and therefore most plugins might not be fully compatible with it. Updating to meet the new standards while maintaining the latest compatibility can be a challenge.

You can refer to the help doc on the PHPMailer wiki with a section devoted to the above.

Here is the extract of the quick workaround mentioned in the above link, which will help you to 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 in the php.ini file globally.

This solution will work with the new version of PHPMailer.


Possible Problem 2: Using Godaddy As The Hosting Provider

If you are running your code on GoDaddy and trying to connect to a third-party SMTP provider like smtp.pepipost.com or smtp.sendgrid.com, you might get an error like the one below:

Mailer Error: SMTP connect() failed.

This is because GoDaddy has explicitly blocked the outgoing SMTP connection to ports 25, 587, and 465 to all external servers except their own. This is primarily to avoid the usage of any third-party SMTP. 

Below are a few workarounds to avoid SMTP connection issues in GoDaddy:

a)- Use GoDaddy SMTP instead of any third party:

GoDaddy SMTP is best when you want to send 1-1 personalized emails. For that, just make the following changes in your PHPMailer code, and you will be done;

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

Note: GoDaddy also restricts free domains such as Gmail, Yahoo, Hotmail, outlook, live, aim, or MSN as sender domain/From address. This is to avoid  forging or spoofing the address without having custom SPF and DKIM.

If you want to send bulk emails, this option will increase the chances of your emails landing in spam and your domain/IP address getting blacklisted. In such a case, checking your email blacklist status and going with option no 2 is suggested.

b)- Use email APIs instead of any SMTP:

GoDaddy does not block the outgoing HTTP ports (80, 8080). Therefore, we recommend using a good third-party email service provider that provides 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, email APIs will give you better control of 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 a third-party SMTP provider like smtp.pepipost.com or smtp.sendgrid.com, you might get an error like the one below:

SMTP connect() failed.

This occurs mostly due to the firewall rules, 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 emails.

The solution to this is to either use email APIs instead of SMTP or contact the hosting provider to allow connection to SMTP ports 25, 587, and 465.

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

1. Try telnet command
By using the telnet command, you can 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).

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.

telnet: connect to address Connection refused
telnet: Unable to connect to remote host

2. Use outPorts

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

Possible Problem 4: SELinux Blocking Issue

If you face the following error:

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

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

This problem mostly involves Linux-based machines like RedHat, Fedora, Centos, etc.

How to debug the SELinux issue that is blocking these SMTP connections?

You can use the ‘getsebool’ command to check whether the HTTP daemon can 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

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; lack of SSL is one of them.

There might be a case that the OpenSSL extension is not enabled in your php.ini. To confirm whether this is creating the connection problem, you need to enable the extension=php_openssl.dll in the ini file. 

PHPMailer also gives a functionality by which you can get detailed logs of the SMTP connection. You can enable it by including the following code in your script;

$mail->SMTPDebug = 2;

By setting the value of the SMTPDebug property to 2, you will get both server and client-level transcripts.

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

If you are using GoDaddy hosting, enabling SSL might not fix your problem. Here is an in-depth troubleshooting reference. Refer to the above GoDaddy section.)


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

There are few hosting companies which provide IPv6 connectivity. However, they explicitly block outgoing SMTP connections over IPv6 and allow the same over IPv4.

This issue could be resolved 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; you can disable the check-in SMTPOptions.
This is a worst-case scenario. Often, it's the port blocked by the provider.
So, it is important first to confirm whether the port is unblocked 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, checking the sendmail_path in your php.ini file is important. Ideally, your sendmail_path should point to the sendmail binary (usually, the default path is /usr/sbin/sendmail).

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

If it's not a configuration problem, try further debugging and check whether you have a local mail server installed and configured properly. You can install any good mail server like Postfix.

Note: If all of the above are already in place and you're still getting this error of "Could not instantiate mail function," then try getting more details of the error. If you see "More than one from person"as the error message, then it means that in php.ini the sendmail_path property already contains a from-fparameter. Therefore, you will get an error since your code is trying to add a second envelope.


What Is The Use Of IsSMTP()?

isSMTP() is being used when you want to tell PHPMailer class to use the custom SMTP configuration 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 = "[email protected]"; // SMTP username
           $mail->Password = "mypasswword"; // SMTP password
           $Mail->Priority = 1;
           $mail->AddAddress("[email protected]","Name");
           $mail->SetFrom($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."

You can try various solutions listed above as per the PHPmailer version and hosting provider to resolve the error and start sending your emails.

If you still have issues, here are some related resources.

We are a community of email enthusiasts. If you have further queries, drop an email to [email protected], and we would be happy to help you.

Excited about the latest in Bulk Email Marketing! Check out this insightful blog on Gmail and Yahoo updates in the email marketing landscape.
Explore the Blog - Here
Stay ahead of the game with valuable insights on optimizing your email campaigns! 📬

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Dibya Sahoo🥑

Co-founder, Pepipost

Over 12 years of experience, building products to deliver emails ~ 🥑 Developer Relations Guy ~ 🎙️ Speaker

You can also explore

Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.

Deploy emails that are
screenshot worthy!

Stop settling for static emails - use Netcore's platform to create dynamic, interactive emails that convert.
Let's Deploy
Get Started Now