[Tutorial] PHPMailer SMTP Error: Could not connect to SMTP host.
Solve this common SMTP error with PHPMailer.
Join the DZone community and get the full member experience.
Join For FreeHave you encountered an error that says, “PHPMailer SMTP Error: Could not connect to SMTP host?”
Let’s solve it together.
PHPMailer is one of the most popular open source libraries for sending emails with PHP. While it’s easy to deploy and start sending emails, 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:
You may also like: A Complete Guide to Laravel 5.8 Installation.
SMTP Error: Could Not Connect to SMTP Host
Depending on your situation, there can be multiple reasons why this error occurs. So, please try to go through the different scenarios below and pick the one that is closest to your use case.
Possible Problem One: 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 behavior, which has caused this problem.
Here is a help doc on PHPMailer wiki that has a section on this issue.
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 Two: 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 there's really nothing to debug. This occurs 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; many have expressed their frustration in form of issues on StackOverflow as well.
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 forge 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 landing in spam. In such a case, I would suggest going with a second option.
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 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 that 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 it can also create a really frustrating situation for developers like us.
The only solution to this is the same as I suggested above in the Godaddy section (Use Email APIs instead of any SMTP). Additionally, you could contact the hosting provider to allow connection to SMTP ports.
You might be asking, "How do I check whether your outgoing port (25, 587 or 465) is really blocked or not?"
- Trying doing telnet: Using telnet command you can actually test whether the port is opened or not. If Port 25 is not blocked, you will get a successful 220 response (text may vary). If Port 25 is blocked, you will get a connection error or no response at all.
//Type the following command to see if Port 25 is blocked on your network.
telnet pepipost.com 25
Trying 202.162.247.93... Connected to pepipost.com. Escape character is '^]'. 220 pepipost.com ESMTP Postfix
Trying 202.162.247.93... telnet: connect to address 202.162.247.93: Connection refused telnet: Unable to connect to remote host
- Use outPorts: outPorts is a very good open source project on GitHub that 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, most it is most likely that SELinux is preventing PHP or the webserver from sending emails.
This problem occurs often 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.
SMTP connect() failed.
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 the lack of SSL is often a contributing factor.
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, you should 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.
Possible Problem 6: PHPMailer Unable to Connect to SMTP Because ff the IPv6 Blocking Issue
There are some set of newer hosting companies, including DigitalOcean, that provide IPv6 connectivity but explicitly block outgoing SMTP connections over IPv6 but allow the same over IPv4.
You can work around this issue 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.
This is mostly an extreme case. Most of the time, 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 used when you want to tell the 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 following 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.
If you like this tutorial, do like and share, and feel free to comment if you have any questions.
Related Articles
Opinions expressed by DZone contributors are their own.
Comments