Failed to connect to server 110 connection timed out smtp server error failed to connect

Problem description I have been using an old version of phpmailer for many years with success (on a shared server) with the following code: Please check http://www.crmpt.com/email_test_send_old.php...

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.

Already on GitHub?
Sign in
to your account

Comments

@pipesportugal

Problem description

I have been using an old version of phpmailer for many years with success (on a shared server) with the following code:
Please check http://www.crmpt.com/email_test_send_old.php

Code to reproduce

<?php
require_once("phpmailer/class.phpmailer.php");  
$mail = new PHPMailer();  
$mail->IsSMTP();  // telling the class to use SMTP  
$mail->Host     = "localhost"; // SMTP server  
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "2e@afaturar.com";  // SMTP username
$mail->Password = "TemporaryPass123"; // SMTP password 
$mail->From = "2e@afaturar.com";
$mail->FromName = "TEST COMPANY";
$mail->AddReplyTo("ca@mz2e.com","2E");
$mail->AddCC("jose.jc.almeida@hotmail.com"); // name is optional
$mail->AddBCC("jose.jc.almeida@gmail.com");  // name is optional
$emailtosend = "ca@mz2e.com";
$mail->AddAddress("$emailtosend"); 
$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$mail->IsHTML(true);                                  // set email format to HTML
$mail->Subject = "EMAIL TEST";
$mail->Body    = nl2br("EMAIL TEST ** EMAIL TEST ** EMAIL TEST **");  
$mail->AltBody = nl2br("EMAIL TEST ** EMAIL TEST ** EMAIL TEST **");

if(!$mail->Send()) {  
 echo "<br>Email not sent !!<br> ERRO: $mail->ErrorInfo<br>"; 
 echo "<br>mail->From = $mail->From<br>"; 
 echo "<br>mail->AddAddress = $emailtosend<br>"; 
}  
?>

Debug output

1 or 2 weeks ago, my provider must have changed some server settings (I don’t know) and I started receiving:
SMTP Error: Could not connect to SMTP host.

PRESENT SITUATION:

Problem description

Because of the above problem, I moved to phpmailer6, but still having no success.
please check:
http://www.crmpt.com/email_test_send_new.php

Code to reproduce

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;
require 'phpmailer6/src/PHPMailer.php';
require 'phpmailer6/src/SMTP.php';
require 'phpmailer6/src/Exception.php';
$mail = new PHPMailer(true);  
try {
     $mail->SMTPDebug = 2;  
     $mail->isSMTP();  // telling the class to use SMTP  
	 $mail->Host = "mail.afaturar.com"; // SMTP server  
	 $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
     $mail->Port = 587;  
	 $mail->SMTPAuth = true;     // turn on SMTP authentication
     $mail->Username = "2e@afaturar.com";  // SMTP username
     $mail->Password = "TemporaryPass123"; // SMTP password 
	 $mail->setFrom("2e@afaturar.com");
     $emailtosendto = "ca@mz2e.com";
	 $mail->addAddress("$emailtosendto");  
     $mail->addReplyTo("ca@mz2e.com","2E");
     $mail->addCC("jose.jc.almeida@hotmail.com"); // name is optional
     $mail->addBCC("jose.jc.almeida@gmail.com");  // name is optional
     $mail->isHTML(true);                         // set email format to HTML
     $mail->Subject = "EMAIL TEST";
     $mail->Body    = nl2br("EMAIL TEST ** EMAIL TEST ** EMAIL TEST **");  
     $mail->AltBody = nl2br("EMAIL TEST ** EMAIL TEST ** EMAIL TEST **");
     $mail->send();
}
catch (Exception $e) {
    echo 'Message could not be sent(phpmailer6).<br>';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

Debug output

2017-10-29 07:00:47 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent(phpmailer6).
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Thank you so much for all the help.
Regards
Jose.

@Synchro

Well don’t just change the code and hope it works, figure out the cause. It’s most likely that your ISP has started blocking outbound SMTP. Diagnosing this is covered in the troubleshooting guide linked from the error.

@pipesportugal

@Synchro
I have complained to my Hosting provider (vodahost) and their reply was:

Hello,

I’ve checked and could not find any issues from our end also we didn’t make any changes from our side. It seems the issue with your phpmailer script. Please check with the script from your and let me know if any changes need to be made from our end.

Regards,

The above is the configuration recommended by vodahost:

imagem

According to their instructions, I changed the following part of my code accordingly:

$mail->Host = "voda4.vodahost4.com"; // SMTP server  
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; //587 tls  

I also tested the ssl and it is loaded. Please check:
http://www.crmpt.com/teste_ssl.php

This same email is being used to send emails, without any problem from Microsoft Outlook, so if this was to be a block from vodahost on outbond SMTP, it would not be possible for Outlook to perform that task, right ?

Thanks for your help Mr Bointon,

Regards
Jose

2 participants

@Synchro

@pipesportugal

Hi,

Last week, PHPMailer suddenly stopped working. I got the following as the error message when I send the test e-mail:

SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host.

I’ve found the same issue with the module SMTP Authentication Support.

Any idea what could have caused this? Any good suggestions for debugging this?

Tag1 supports the Drupal Project.Tag1 logo

Comments

smk-ka’s picture

Connection time-out means the mail server cannot be reached anymore. If the mail server is different from your web server (i.e. not localhost) I’d check with your hosting provider whether they enabled some sort of firewall that blocks connections on the SMTP port (port 25 usually).

  • Log in or register to post comments

Relia’s picture

Hello I have the same problem.

I set up PHPMailer to send mails trought gmail:

Primary SMTP server:mail.gbt.tfo.upm.es
SMTP Port: 465
Use secure protocol: SSL

User: usergmail@gmail.com
password: gmailpassword

TEst configuration: mailtutest@domain.com

But when I submit clickin save configuration, the next error message appear:

A test e-mail has been sent to fjpeces@gfgf.es. You may want to check the logs for any error messages.
SMTP -> ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host.

I’m using OpenSuSe 10.3, Server Apache2, php 5.2.5.

I think the problem is the ports are closed to outside, but before to communicate with the admin, i’d want read your opinions!

thanks!

  • Log in or register to post comments

LEternity’s picture

I contacted my provider and they fixed the problem. I.e. they screwed up…

  • Log in or register to post comments

Relia’s picture

i fixed the problem, I cotacted admin and the problem was the ports! They are closed!

  • Log in or register to post comments

smk-ka’s picture

Thanks for your feedback, I’ve added a description for this error to README.txt, based on what I said in #1.

  • Log in or register to post comments
Status: Fixed » Closed (fixed)

Automatically closed — issue fixed for 2 weeks with no activity.

  • Log in or register to post comments

ThirstySix’s picture

In localhost SMTP work’s fine, but , site it does not work SMTP.
I give same configuration in SMTP localhost and site.

but I get below error

SMTP -> ERROR: Failed to connect to server: Connection timed out (111)
SMTP Error: Could not connect to SMTP host. 

I changed port number and name….etc..
but i get same error….
how can I slove this?

Thanks in advance..

  • Log in or register to post comments

smk-ka’s picture

@satheeshkumarr Read comment #1. Contact your hosting provider to open specific firewall ports.

  • Log in or register to post comments

Mahadirz’s picture

Version: 6.x-2.1 » 7.x-3.x-dev
Component: Code » Miscellaneous
Status: Closed (fixed) » Active

I also got this same problem before…
Try checking all your files and directories permission. If all your files and folder permission is set to 777 they can’t access to SMTP config.

So reset the permission of all the files to 644 while the folders to 755 .

cheers…
http://www.mahadirlab.com/en

  • Log in or register to post comments

zabalex’s picture

I had the same issue with my host. They told me to try every possible option, stated that it was a problem with my code, email server, application (drupal), etc.
I wasted 2 weeks, than hired a guy to develop firewall ports blocking detection application, and here it is. As soon as I was able to proove to hosting company that they blocked port 465, they did not admin problem. As soon as I showed them this tool, yes, they admitted that they have problem and opened the port.
Here is the tool: Port forwarding tester external server:

Let me know what you think

  • Log in or register to post comments

earth1’s picture

@smk-ka #1. I am using PHPMailer 6.x-2.2. Firewall is not the answer.

error -> SMTP error: could not connect to SMTP host.

The website is using godaddy with jangosmtp as it email system. It’s Primary SMTP server: relay.jangostmp.net and SMTP port: 587. The godaddy representative said go to my MX (Mail Exchanger) and set relay.jangostmp.net. But this didn’t work. I am running out of ideas. Can you help me?

Thanks in advance

  • Log in or register to post comments

zabalex’s picture

I spent so much time on issues like that, I might be able to help you Earth1 if the tool from my previous post doesn’t show port as blocked.
Don’t trust their advise on MX Exchanger. I prooved back to GoDaddy that over 50% of popular organizations don’t set their relays, and their emails still go through fine.
What is your domain? Send me PM if you don’t want to list it here.

  • Log in or register to post comments

keithm’s picture

This problem may be a result of enabling IPv6 support in the operating system. I saw several sites with working PHPMailer configurations break when my host enabled IPv6. PHP has multiple known bugs in dealing with IPv6.

PHPMailer uses the PHP fsockopen() function that has this known bug.

The problem is that fsockopen() doesn’t detect that a connection was actively refused and instead times out on it.

The problem scenario occurs when the host has both ipv4 and ipv6 addresses (e.g,, smtp.gmail.com), the service is only listening on ipv4 but ipv6 has precedence, which results in:

  1. trying IPv6
  2. time out
  3. fail

instead of:

  1. trying IPv6
  2. connection refused
  3. trying IPv4
  4. success

If this is the problem, you can work around it by setting the IP address directly in the PHPMailer settings or in /etc/hosts. The problem still exists (at least) in PHP v5.3.2.

  • Log in or register to post comments

vvenk’s picture

Hi:

I am on a Windows 7 machine and developing an application that has SendEmail as a function using VB.Net and MS Visual Studio 2010.

The code works fine on another Windows 7 machine. The only difference is that the laptop it does not work is a corporate laptop and I am part of the Administrators group on that laptop.

I used the tool and it says the port is open.

Any idea how to resolve the issue?

venki

  • Log in or register to post comments

ravi_dev’s picture

I have another mail server for my site then how can i connect ‘@fsockopen(«www.google.com», 80, $errno, $errstr, 10)’?

  • Log in or register to post comments

Anonymous’s picture

Comment #16

Anonymous (not verified) CreditAttribution: Anonymous commented 7 October 2016 at 08:14

This error is not resolving check the image after saving smtp configuration this screen appears

  • Log in or register to post comments

oadaeh’s picture

Comment #17

oadaeh CreditAttribution: oadaeh as a volunteer commented 7 October 2016 at 13:21

Status: Active » Closed (won’t fix)

@saif-ur-rehman, that error is not from this module, but from the Variable module. You need to start there to resolve the problem. And next time, open a new issue, as this one you posted on is unrelated.

  • Log in or register to post comments

oadaeh’s picture

Comment #18

oadaeh CreditAttribution: oadaeh as a volunteer commented 7 October 2016 at 13:25

@saif-ur-rehman, allow me to clarify what I said about starting a new issue. What you did in searching for an existing issue to post in was a good thing, but the issue you ended up posting in was unrelated to your problem, and so you should have just created a new issue.

  • Log in or register to post comments

oadaeh’s picture

Comment #19

oadaeh CreditAttribution: oadaeh as a volunteer commented 7 October 2016 at 13:26

Status: Closed (won’t fix) » Fixed

I set the wrong status previously, so now I’m fixing that.

  • Log in or register to post comments

oadaeh’s picture

Comment #20

oadaeh CreditAttribution: oadaeh as a volunteer commented 7 October 2016 at 13:26

Status: Fixed » Closed (fixed)
  • Log in or register to post comments

thanhseouc’s picture

My site is not a bug fix as above not overcome

  • Log in or register to post comments

oadaeh’s picture

  • Log in or register to post comments

mailboxes-of-different-colors-and-shape-on-pos

Have 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.

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?»

  1. 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
    1. 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

  • Develop a REST API in PHP.
  • How to Create a Simple and Efficient PHP Cache.

Opinions expressed by DZone contributors are their own.

Понравилась статья? Поделить с друзьями:
  • Failed to configure ics you can connect to softap как исправить
  • Failed to compile stylesheet 1 error detected
  • Failed to clear bcb message failed to find misc partition как исправить
  • Failed to check for updates error 6 faceit
  • Failed to bind to port minecraft server как исправить