Ошибка smtp failed to connect to server connection timed out 110

Am trying to send mail to a gmail address but it keeps on getting this error «SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed.» What could be the problem?

        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@gmail.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  = "Message from  Contact form";
            $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.';
            }

AnFi's user avatar

AnFi

10.5k3 gold badges23 silver badges47 bronze badges

asked Aug 28, 2013 at 19:29

andy's user avatar

4

Remove or comment out the line-

$mail->IsSMTP();

And it will work for you.

I have checked and experimented many answers from different sites but haven’t got any solution except the above solution.

dario's user avatar

dario

5,12912 gold badges28 silver badges32 bronze badges

answered Aug 10, 2015 at 12:51

Snehasis's user avatar

SnehasisSnehasis

9156 silver badges10 bronze badges

17

You must to have installed php_openssl.dll, if you use wampserver it’s pretty easy, search and apply the extension for PHP.

In the example change this:

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

and then you recived an email from gmail talking about to enable the option to Less Safe Access Applications
here https://www.google.com/settings/security/lesssecureapps

I recommend you change the password and encrypt it constantly

answered Oct 8, 2014 at 2:03

El David's user avatar

El DavidEl David

6168 silver badges17 bronze badges

1

You’ve got no SMTPSecure setting to define the type of authentication being used, and you’re running the Host setting with the unnecessary ‘ssl://’ (PS — ssl is over port 465, if you need to run it over ssl instead, see the accepted answer here. Here’s the lines to add/change:

+ $mail->SMTPSecure = 'tls';

- $mail->Host = "ssl://smtp.gmail.com";
+ $mail->Host = "smtp.gmail.com";

Community's user avatar

answered Aug 29, 2013 at 20:58

Dmitri DB's user avatar

Dmitri DBDmitri DB

3173 silver badges15 bronze badges

4

Are you running on Localhost? and have you edit the php.ini ?

If not yet, try this:
1. Open xampp->php->php.ini
2. Search for extension=php_openssl.dll
3. The initial will look like this ;extension=php_openssl.dll
4. Remove the ‘;’ and it will look like this extension=php_openssl.dll
5. If you can’t find the extension=php_openssl.dll, add this line extension=php_openssl.dll.
6. Then restart your Xampp.

Goodluck ;)

answered Jan 11, 2014 at 4:03

Sendoh Akira's user avatar

1

I know its been a while since this question but I had the exact problem and solved it by disabling SMTP_BLOCK on csf.conf (we use CSF for a firewall).

To disable just edit csf.conf and disable SMTP_BLOCK like so:

###############################################################################
# SECTION:SMTP Settings
###############################################################################
# Block outgoing SMTP except for root, exim and mailman (forces scripts/users
# to use the exim/sendmail binary instead of sockets access). This replaces the
# protection as WHM > Tweak Settings > SMTP Tweaks
#
# This option uses the iptables ipt_owner/xt_owner module and must be loaded
# for it to work. It may not be available on some VPS platforms
#
# Note: Run /etc/csf/csftest.pl to check whether this option will function on
# this server
# SMTP_BLOCK = "1" --> this will cause phpmailer Connection timed out (110)
SMTP_BLOCK = "0"

answered Jan 23, 2016 at 20:32

Goldbug's user avatar

GoldbugGoldbug

6076 silver badges8 bronze badges

1

i’ve had this problem in tell i recive an email from google telling me that someone try to login to your account is it you and i answer yes then it start workin so if this is the case for you look in your email and allow the server

answered Jul 16, 2016 at 12:59

user5778000's user avatar

2

Login your Google account at myaccount.google.com/security go to «Login» and then «Security», scroll to bottom then enable the «Allow less secure apps» option.

Daniel's user avatar

Daniel

1,22914 silver badges24 bronze badges

answered Dec 27, 2016 at 22:07

Hieu.Gi's user avatar

Here is a list of this you should look into when dealing with PHPMailer:

  1. Enable openSSL by un-commenting extension=php_openssl.dll in your PHP.ini
  2. Use $mail->SMTPSecure = 'tls'; and $mail->Port = 587;
  3. Enable debugging for if you are going wrong somewhere else like incorrect username and password etc.

answered May 14, 2015 at 7:02

KKK's user avatar

KKKKKK

3,1595 gold badges35 silver badges58 bronze badges

You are doing all well. Just you have to check different SMTP ports like 465 and others that works on your system.
Another thing to keep in mind to allow access to the less secure apps by google account otherwise it throws the same error.
I have gone through it for a whole day and the only thing I am doing wrong is the port no., I just changed the port no. and it works.

answered Mar 7, 2017 at 13:59

Deepak Kumar's user avatar

Deepak KumarDeepak Kumar

6771 gold badge8 silver badges22 bronze badges

Mailjet

SMTP SETTINGS

Port: 25 or 587 (some providers block port 25)

I work by changing the port after deploying the app to the server.

  • In Debug it worked for me: $mail->Port = 25;
  • In Release it worked for me: $mail->Port = 587;

GL

answered Dec 19, 2018 at 2:13

Braian Coronel's user avatar

Braian CoronelBraian Coronel

21.9k4 gold badges56 silver badges60 bronze badges

To get it working, I had to go to myaccount.google.com -> «connected apps & sites», and turn «Allow less secure apps» to «ON» (near the bottom of the page).

answered Aug 1, 2017 at 9:19

Akif Hussain Sayyed's user avatar

If it works on your localhost but not on your web host:

Some hosting sites block certain outbound SMTP ports. Commenting out the line $mail->IsSMTP(); as noted in the accepted answer may make it work, but it is simply disabling your SMTP configuration, and using the hosting site’s email config.

If you are using GoDaddy, there is no way to send mail using a different SMTP. I was using SiteGround, and found that they were allowing SMTP access from ports 25 and 465 only, with an SSL encryption type, so I would look up documentation for your host and go from there.

answered Apr 6, 2018 at 22:06

Stephanie's user avatar

StephanieStephanie

1332 silver badges7 bronze badges

        <?php
    require 'PHPMailer/PHPMailerAutoload.php';
    
    $mail = new PHPMailer();
    
    $mail->SMTPDebug = 0;                               // Enable verbose debug output
    
    
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->IsSMTP();
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'mail@gmail.com';                 // SMTP username
    $mail->Password = 'your pass';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->setFrom('mail@gmail.com');
    $mail->addAddress('mail@gmail.com');               // Name is optional
    
    
    $mail->isHTML(true);                                  // Set email format to HTML
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    
  ?>

THIS WORK FOR ME JUST WHEN YOU WANT TO TEST
DON’T USE IDE FOR TESTING THAT BECAUSE PROBABLY NOT WORK IF YOU IN LOCALHOST USE localhost/yourfile.php

answered Mar 17, 2021 at 16:56

MAX's user avatar

the solution is configure the gmail preferences, access to no secure application

CSchulz's user avatar

CSchulz

10.8k10 gold badges59 silver badges113 bronze badges

answered Jun 6, 2016 at 3:12

marcelo's user avatar

0

Am trying to send mail to a gmail address but it keeps on getting this error «SMTP -> ERROR: Failed to connect to server: Connection timed out (110)SMTP Connect() failed. Message was not sent.Mailer error: SMTP Connect() failed.» What could be the problem?

        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@gmail.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  = "Message from  Contact form";
            $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.';
            }

AnFi's user avatar

AnFi

10.5k3 gold badges23 silver badges47 bronze badges

asked Aug 28, 2013 at 19:29

andy's user avatar

4

Remove or comment out the line-

$mail->IsSMTP();

And it will work for you.

I have checked and experimented many answers from different sites but haven’t got any solution except the above solution.

dario's user avatar

dario

5,12912 gold badges28 silver badges32 bronze badges

answered Aug 10, 2015 at 12:51

Snehasis's user avatar

SnehasisSnehasis

9156 silver badges10 bronze badges

17

You must to have installed php_openssl.dll, if you use wampserver it’s pretty easy, search and apply the extension for PHP.

In the example change this:

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

and then you recived an email from gmail talking about to enable the option to Less Safe Access Applications
here https://www.google.com/settings/security/lesssecureapps

I recommend you change the password and encrypt it constantly

answered Oct 8, 2014 at 2:03

El David's user avatar

El DavidEl David

6168 silver badges17 bronze badges

1

You’ve got no SMTPSecure setting to define the type of authentication being used, and you’re running the Host setting with the unnecessary ‘ssl://’ (PS — ssl is over port 465, if you need to run it over ssl instead, see the accepted answer here. Here’s the lines to add/change:

+ $mail->SMTPSecure = 'tls';

- $mail->Host = "ssl://smtp.gmail.com";
+ $mail->Host = "smtp.gmail.com";

Community's user avatar

answered Aug 29, 2013 at 20:58

Dmitri DB's user avatar

Dmitri DBDmitri DB

3173 silver badges15 bronze badges

4

Are you running on Localhost? and have you edit the php.ini ?

If not yet, try this:
1. Open xampp->php->php.ini
2. Search for extension=php_openssl.dll
3. The initial will look like this ;extension=php_openssl.dll
4. Remove the ‘;’ and it will look like this extension=php_openssl.dll
5. If you can’t find the extension=php_openssl.dll, add this line extension=php_openssl.dll.
6. Then restart your Xampp.

Goodluck ;)

answered Jan 11, 2014 at 4:03

Sendoh Akira's user avatar

1

I know its been a while since this question but I had the exact problem and solved it by disabling SMTP_BLOCK on csf.conf (we use CSF for a firewall).

To disable just edit csf.conf and disable SMTP_BLOCK like so:

###############################################################################
# SECTION:SMTP Settings
###############################################################################
# Block outgoing SMTP except for root, exim and mailman (forces scripts/users
# to use the exim/sendmail binary instead of sockets access). This replaces the
# protection as WHM > Tweak Settings > SMTP Tweaks
#
# This option uses the iptables ipt_owner/xt_owner module and must be loaded
# for it to work. It may not be available on some VPS platforms
#
# Note: Run /etc/csf/csftest.pl to check whether this option will function on
# this server
# SMTP_BLOCK = "1" --> this will cause phpmailer Connection timed out (110)
SMTP_BLOCK = "0"

answered Jan 23, 2016 at 20:32

Goldbug's user avatar

GoldbugGoldbug

6076 silver badges8 bronze badges

1

i’ve had this problem in tell i recive an email from google telling me that someone try to login to your account is it you and i answer yes then it start workin so if this is the case for you look in your email and allow the server

answered Jul 16, 2016 at 12:59

user5778000's user avatar

2

Login your Google account at myaccount.google.com/security go to «Login» and then «Security», scroll to bottom then enable the «Allow less secure apps» option.

Daniel's user avatar

Daniel

1,22914 silver badges24 bronze badges

answered Dec 27, 2016 at 22:07

Hieu.Gi's user avatar

Here is a list of this you should look into when dealing with PHPMailer:

  1. Enable openSSL by un-commenting extension=php_openssl.dll in your PHP.ini
  2. Use $mail->SMTPSecure = 'tls'; and $mail->Port = 587;
  3. Enable debugging for if you are going wrong somewhere else like incorrect username and password etc.

answered May 14, 2015 at 7:02

KKK's user avatar

KKKKKK

3,1595 gold badges35 silver badges58 bronze badges

You are doing all well. Just you have to check different SMTP ports like 465 and others that works on your system.
Another thing to keep in mind to allow access to the less secure apps by google account otherwise it throws the same error.
I have gone through it for a whole day and the only thing I am doing wrong is the port no., I just changed the port no. and it works.

answered Mar 7, 2017 at 13:59

Deepak Kumar's user avatar

Deepak KumarDeepak Kumar

6771 gold badge8 silver badges22 bronze badges

Mailjet

SMTP SETTINGS

Port: 25 or 587 (some providers block port 25)

I work by changing the port after deploying the app to the server.

  • In Debug it worked for me: $mail->Port = 25;
  • In Release it worked for me: $mail->Port = 587;

GL

answered Dec 19, 2018 at 2:13

Braian Coronel's user avatar

Braian CoronelBraian Coronel

21.9k4 gold badges56 silver badges60 bronze badges

To get it working, I had to go to myaccount.google.com -> «connected apps & sites», and turn «Allow less secure apps» to «ON» (near the bottom of the page).

answered Aug 1, 2017 at 9:19

Akif Hussain Sayyed's user avatar

If it works on your localhost but not on your web host:

Some hosting sites block certain outbound SMTP ports. Commenting out the line $mail->IsSMTP(); as noted in the accepted answer may make it work, but it is simply disabling your SMTP configuration, and using the hosting site’s email config.

If you are using GoDaddy, there is no way to send mail using a different SMTP. I was using SiteGround, and found that they were allowing SMTP access from ports 25 and 465 only, with an SSL encryption type, so I would look up documentation for your host and go from there.

answered Apr 6, 2018 at 22:06

Stephanie's user avatar

StephanieStephanie

1332 silver badges7 bronze badges

        <?php
    require 'PHPMailer/PHPMailerAutoload.php';
    
    $mail = new PHPMailer();
    
    $mail->SMTPDebug = 0;                               // Enable verbose debug output
    
    
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->IsSMTP();
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'mail@gmail.com';                 // SMTP username
    $mail->Password = 'your pass';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to
    $mail->setFrom('mail@gmail.com');
    $mail->addAddress('mail@gmail.com');               // Name is optional
    
    
    $mail->isHTML(true);                                  // Set email format to HTML
    
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
    
    if(!$mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }
    
  ?>

THIS WORK FOR ME JUST WHEN YOU WANT TO TEST
DON’T USE IDE FOR TESTING THAT BECAUSE PROBABLY NOT WORK IF YOU IN LOCALHOST USE localhost/yourfile.php

answered Mar 17, 2021 at 16:56

MAX's user avatar

the solution is configure the gmail preferences, access to no secure application

CSchulz's user avatar

CSchulz

10.8k10 gold badges59 silver badges113 bronze badges

answered Jun 6, 2016 at 3:12

marcelo's user avatar

0

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

I am using phpmailer to send email. It gives me an error. Here is the code

$mail = new PHPMailer();
    $mail->IsSMTP(); 
    $mail->SMTPDebug  = 2;                     
    $mail->SMTPAuth   = true;                  
    $mail->SMTPSecure = "ssl";                 
    $mail->Host       = "smtp.gmail.com";      
    $mail->Port       = 465;             
    $mail->AddAddress($email);
    $mail->Username="ajaynewnew@gmail.com";  
    $mail->Password="mypassword";
    $mail->SetFrom('user@gmail.com','name');
    $mail->AddReplyTo("user95@gmail.com"," name");
    $mail->Subject    = $subject;
    $mail->MsgHTML($message);
    $mail->Send();

asked Apr 6, 2016 at 7:30

swdpankaj's user avatar

3

try with this code

$mail = new PHPMailer(true);
$mail->IsSMTP(); 
$mail->SMTPDebug  = 2;                     
$mail->SMTPAuth   = true;                
$mail->Host       = "ssl://smtp.gmail.com";      
$mail->Port       = 465;             
$mail->AddAddress($email);
$mail->Username="ajaynewnew@gmail.com";  
$mail->Password="mypassword";
$mail->SetFrom('user@gmail.com','name');
$mail->AddReplyTo("user95@gmail.com"," name");
$mail->Subject    = $subject;
$mail->MsgHTML($message);
$mail->Send();

answered Apr 6, 2016 at 7:43

sagar patel's user avatar

sagar patelsagar patel

5915 silver badges18 bronze badges

4

These days, an option to send mails is a basic requirement of any web application.

So popular applications like WordPress, Drupal etc. include a mail program called “PHPMailer” for sending mails.

The steps to setup PHPMailer may not be intuitive to many website owners and mistakes in configuration often cause “Smtp error: Failed to connect to server” error.

As part of our Support Services, we help website owners solve their technical issues. And, mail issue related with PHPMailer is an error that we see often .

In this article, we’ll see the top reasons for “Smtp error: Failed to connect” and how we fix them.

What is “Smtp error: Failed to connect to server” ?

Spammers often use php scripts that directly connect to remote servers and send spam mails.

To defend this, many Web Hosting providers block direct connection from websites to external mail servers.

In such servers, mails from website can be sent only via its own mail server (SMTP server) port, just as how Outlook or Windows Mail works.

PHPMailer is a mail application that works like a mail client and helps to send mail via SMTP server.

But, PHPMailer do not work out of the box. It can fail due to firewall restrictions on the server, wrong mail server name, port etc. and shows the error:

“Smtp error: Failed to connect to server”

And, depending on the response from the mail server, we’ve seen 2 variations of this error :

SMTP ERROR: Failed to connect to server: Connection refused (111)

or

SMTP ERROR: Failed to connect to server: Connection timed out (110)

What causes SMTP ERROR: Failed to connect to server ?

Here, let us discuss the top reasons for “SMTP ERROR: Failed to connect to server”.

1. SMTP restrictions on the server.

Servers restrict the programs that can directly connect to remote servers and send mail. Usually, only mail server, root user etc. allow SMTP connections.

For example, CPanel servers block access to external SMTP servers using the “SMTP Restrictions” option.

With this restriction, connection from PHPMailer to an external mail server do not work. The connection wait for some time and eventually die with the following error:

2018-10-12 04:12:37 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Oops! Something went wrong and we couldn't send your message.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

2. Firewall restrictions on the server

Mail servers accept or reject connections based on certain firewall policies.

All mail servers allow the connection from default mail port 25. Bu,t other mail ports like 465, 587 etc. will not be open in many servers.

On a server with mail port restrictions, when a website owner tries to send mail using an external smtp server on port 465, it ends up in error:

2018-08-28 10:33:12 Connection: Failed to connect to server. Error number 2. "Error notice: stream_socket_client(): unable to connect to ssl://mail.xyz.com:465 (Connection refused)
2018-08-28 10:33:12 SMTP ERROR: Failed to connect to server: Connection refused (111)

Here, this “Connection Refused” error means that sending mail server refuses outbound connections on port 465 and is not able to connect to remote mail server.

3. Incorrect settings in PHPMailer

This SMTP error can also happen if the mail server name is incorrectly set (with additional white space) in PHPMailer configuration. Then, web form tries to connect to an invalid name and fails.

4. DNS failures

For the PHPMailer to work properly, the mail server specified in its configuration should have proper dns records. When dns do not work on the server, a look up from the server shows wrong IP address or no IP address for the mail server. Again, that causes mail to fail with SMTP error.

How to fix SMTP Error: Failed to connect to server

For mails to work with PHPMailer, both sending and receiving server has to accept connections.

Our Support Engineers primarily checks the connection between mail servers and find whether it is an incoming or outgoing block.

Then, to fix the mail error, we make changes on the server that includes the following :

  1. Modify the firewall rules on the server to allow outbound connections on ports like 465.
  2. Modify the SMTP restrictions on the server. Then, add particular website user to the list of users who can make outbound SMTP connections.
  3. Edit PHPMailer settings like Host, Port etc.
  4. Correct DNS resolution for mail server.

Conclusion

“SMTP ERROR: Failed to connect to server” mainly happens because of mail server connectivity issues, wrong port settings etc. Here, we have discussed the causes that our Support Engineers often see in servers and how we fix them.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = «owonCMyG5nEQ0aD71QM»;

  • Ошибка smtp error code 550
  • Ошибка sms скания 6157285
  • Ошибка sms mms xiaomi redmi
  • Ошибка sms 63731 скания
  • Ошибка smcy 24 симсити