Smtp error failed to connect to server connection refused 111 smtp connect failed

I'm new to PHPMailer, and I just downloaded it with Composer and coded this as index.php: <?php require_once 'vendor/autoload.php'; use PHPMailerPHPMailerPHPMailer; $m = new PHPMailer; $...

I’m new to PHPMailer, and I just downloaded it with Composer and coded this as index.php:

    <?php 
require_once 'vendor/autoload.php';
use PHPMailerPHPMailerPHPMailer;
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 2;

$m->Host = 'smtp.mail.yahoo.com';
$m->Username = 'vagefipooya@yahoo.com';
$m->Password = 'MY PASSWORD';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->IsHTML(true);

$m->SetFrom('pouyavey@gmail.com');
$m->FromName = 'Pouya Vaghefi';
$m->addReplyTo('pouyavey@gmail.com','Pouya Vey');
$m->addAddress('pouyavey@gmail.com','Pouya Vey');
//$m->addCC('alex@phpacademy','Alex Garret');
//$m->addBCC('alex@phpacademy','Alex Garret');
$m->CharSet = "UTF-8";

$m->Subject = 'Here is an email';
$m->msgHTML("convert HTML into a basic plain-text alternative body");
$m->Body = 'This is the body of an email';
$m->AltBody = 'This is the body of an email';

if (!$m->send()) {
        echo "Mailer Error: " . $m->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    ?>

Then I uploaded it to my site (my site does not use ssl) which is using cPanel and tried to load the page but I got this as error:

2018-04-19 10:03:46 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. /wiki/Troubleshooting
Mailer Error: SMTP connect() failed.

I also read the related questions to this problem and changed the port from 465 to 587 (with tls), 25 and 26 but couldn’t solve the problem yet.

So can you please help me with this error, cause I really don’t know what to do!

Thanks…

asked Apr 19, 2018 at 10:08

4

This is mostly due to your hosting providers firewall issues. See on below link where someone had similar issue —

https://github.com/PHPMailer/PHPMailer/issues/295

Contact your hosting provider, they will be able to help you

answered Apr 23, 2018 at 13:31

mdeora's user avatar

mdeoramdeora

3,9502 gold badges16 silver badges29 bronze badges

I tried your code with my email and token, also not work, it shows :

2018-04-28 13:52:41     SMTP ERROR: Failed to connect to server:  (0)
2018-04-28 13:52:41     SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

then i changed below two lines:

$m->SMTPSecure = 'ssl';
$m->Port = 465;

to

$m->SMTPSecure = 'tls';
$m->Port = 587;

then, it works

    ...
2018-04-28 13:53:13     SERVER -> CLIENT: 354 Start mail input; end with <CRLF>.<CRLF>
2018-04-28 13:53:13     CLIENT -> SERVER: Date: Sat, 28 Apr 2018 21:53:04 +0800
2018-04-28 13:53:13     CLIENT -> SERVER: To: "feiffy" <example@example.com>
2018-04-28 13:53:13     CLIENT -> SERVER: From: feiffy <feifeifanye@hotmail.com>
2018-04-28 13:53:13     CLIENT -> SERVER: Reply-To: "feiffy" <example@example.com>
2018-04-28 13:53:13     CLIENT -> SERVER: Subject: Here is an email
2018-04-28 13:53:13     CLIENT -> SERVER: Message-ID: <hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o@pc>
2018-04-28 13:53:13     CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-04-28 13:53:13     CLIENT -> SERVER: MIME-Version: 1.0
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-04-28 13:53:13     CLIENT -> SERVER:       boundary="b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o"
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is the body of an email
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is the body of an email
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o--
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: .
2018-04-28 13:53:14     SERVER -> CLIENT: 250 2.0.0 OK <hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o@pc> [Hostname=SG2PR06MB0776.apcprd06.prod.outlook.com]
2018-04-28 13:53:14     CLIENT -> SERVER: QUIT
2018-04-28 13:53:14     SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel

hope to help you.

answered Apr 28, 2018 at 14:07

feiffy's user avatar

I’ve been using mailgun and I love it. Mailgun.com Totally free for a case like this.

As a suggestion, can you see if there’s a sendmail daemon running on your box? Maybe that will be good enough for your use case?

answered Apr 25, 2018 at 21:35

Lucas's user avatar

LucasLucas

4613 silver badges7 bronze badges

1

I’m new to PHPMailer, and I just downloaded it with Composer and coded this as index.php:

    <?php 
require_once 'vendor/autoload.php';
use PHPMailerPHPMailerPHPMailer;
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 2;

$m->Host = 'smtp.mail.yahoo.com';
$m->Username = 'vagefipooya@yahoo.com';
$m->Password = 'MY PASSWORD';
$m->SMTPSecure = 'ssl';
$m->Port = 465;
$m->IsHTML(true);

$m->SetFrom('pouyavey@gmail.com');
$m->FromName = 'Pouya Vaghefi';
$m->addReplyTo('pouyavey@gmail.com','Pouya Vey');
$m->addAddress('pouyavey@gmail.com','Pouya Vey');
//$m->addCC('alex@phpacademy','Alex Garret');
//$m->addBCC('alex@phpacademy','Alex Garret');
$m->CharSet = "UTF-8";

$m->Subject = 'Here is an email';
$m->msgHTML("convert HTML into a basic plain-text alternative body");
$m->Body = 'This is the body of an email';
$m->AltBody = 'This is the body of an email';

if (!$m->send()) {
        echo "Mailer Error: " . $m->ErrorInfo;
    } else {
        echo "Message sent!";
    }
    ?>

Then I uploaded it to my site (my site does not use ssl) which is using cPanel and tried to load the page but I got this as error:

2018-04-19 10:03:46 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. /wiki/Troubleshooting
Mailer Error: SMTP connect() failed.

I also read the related questions to this problem and changed the port from 465 to 587 (with tls), 25 and 26 but couldn’t solve the problem yet.

So can you please help me with this error, cause I really don’t know what to do!

Thanks…

asked Apr 19, 2018 at 10:08

4

This is mostly due to your hosting providers firewall issues. See on below link where someone had similar issue —

https://github.com/PHPMailer/PHPMailer/issues/295

Contact your hosting provider, they will be able to help you

answered Apr 23, 2018 at 13:31

mdeora's user avatar

mdeoramdeora

3,9502 gold badges16 silver badges29 bronze badges

I tried your code with my email and token, also not work, it shows :

2018-04-28 13:52:41     SMTP ERROR: Failed to connect to server:  (0)
2018-04-28 13:52:41     SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

then i changed below two lines:

$m->SMTPSecure = 'ssl';
$m->Port = 465;

to

$m->SMTPSecure = 'tls';
$m->Port = 587;

then, it works

    ...
2018-04-28 13:53:13     SERVER -> CLIENT: 354 Start mail input; end with <CRLF>.<CRLF>
2018-04-28 13:53:13     CLIENT -> SERVER: Date: Sat, 28 Apr 2018 21:53:04 +0800
2018-04-28 13:53:13     CLIENT -> SERVER: To: "feiffy" <example@example.com>
2018-04-28 13:53:13     CLIENT -> SERVER: From: feiffy <feifeifanye@hotmail.com>
2018-04-28 13:53:13     CLIENT -> SERVER: Reply-To: "feiffy" <example@example.com>
2018-04-28 13:53:13     CLIENT -> SERVER: Subject: Here is an email
2018-04-28 13:53:13     CLIENT -> SERVER: Message-ID: <hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o@pc>
2018-04-28 13:53:13     CLIENT -> SERVER: X-Mailer: PHPMailer 6.0.5 (https://github.com/PHPMailer/PHPMailer)
2018-04-28 13:53:13     CLIENT -> SERVER: MIME-Version: 1.0
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-04-28 13:53:13     CLIENT -> SERVER:       boundary="b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o"
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is a multi-part message in MIME format.
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is the body of an email
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o
2018-04-28 13:53:13     CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: This is the body of an email
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: --b1_hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o--
2018-04-28 13:53:13     CLIENT -> SERVER:
2018-04-28 13:53:13     CLIENT -> SERVER: .
2018-04-28 13:53:14     SERVER -> CLIENT: 250 2.0.0 OK <hW4npgJlHQ2CjCqR42xK7j7BRpAzEFAz8mnwK4G6o@pc> [Hostname=SG2PR06MB0776.apcprd06.prod.outlook.com]
2018-04-28 13:53:14     CLIENT -> SERVER: QUIT
2018-04-28 13:53:14     SERVER -> CLIENT: 221 2.0.0 Service closing transmission channel

hope to help you.

answered Apr 28, 2018 at 14:07

feiffy's user avatar

I’ve been using mailgun and I love it. Mailgun.com Totally free for a case like this.

As a suggestion, can you see if there’s a sendmail daemon running on your box? Maybe that will be good enough for your use case?

answered Apr 25, 2018 at 21:35

Lucas's user avatar

LucasLucas

4613 silver badges7 bronze badges

1

@ndub

Sorry if this is a road heavily traveled. I’ve seen the other posts about this but nothing in them has either solved the problem I’m having or ignited a lightbulb that helped me solve it myself.

Here’s my code:

require 'PHPMailerAutoload.php';
$config = parse_ini_file('/path/to/file/config.ini', true);
$mail = new PHPMailer;
$mail->SMTPDebug = 3;
$mail->Debugoutput = 'html';
$mail->isSMTP();
$mail->Host = $config['host']; //smtp.office365.com
$mail->SMTPAuth = true;
$mail->Username = $config['username']; //an.existing.account@appinc.co
$mail->Password = $config['password']; //confirmed this is being passed correctly
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->From = $config['username'];
$mail->FromName = 'Website Forms';
$mail->addAddress('sales@appinc.co', 'Some Name');
$mail->addReplyTo('sender.email@somedomain.com', 'SenderFirst SenderLast');
$mail->addBCC('my.email.address@appinc.co');
$mail->isHTML(true);
$mail->Subject = 'Contact Form Submission';
$mail->Body = 'Some html here';
$mail->AltBody = 'Some alt content here';
if(!$mail->send()) {
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    //perform success actions
    exit();
}

I’ve confirmed that the domain, username and password are all correct and being passed in correctly. Important to note that this worked on our local dev server prior to launch. Once the site was moved to our hosting account (Hostgator) is when it stopped working. I’ve confirmed with HG that port 587 is open on our server.

Here is the error message I’m seeing:

Connection: opening to smtp.office365.com:587, t=10, opt=array ()
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed.
Message could not be sent.Mailer Error: SMTP connect() failed.

Any help that can be provided is very much appreciated, even if it’s just a link to an article that explains why it won’t work now that it’s in our production environment.

[EDIT]: Minor grammatical error.

@drmad

This sounds like a network/sysadmin problem, not a PHPMailer problem. Try a

telnet smtp.office365.com 587

in your console, for checking conectivity. If everything is working ok, it should write a message like ‘ESMTP MAIL Service ready’. Otherwise that server is blocking yours.

@ndub

@drmad Thanks!

I’ve checked the 587 ports on both servers — mine and smtp.office365.com — with telnet and nmap. They both appear to be open and responding. Because I’m on a shared hosting account (Hostgator, cPanel), I don’t have terminal access on my web server (that I can find), unless you know some way for me to run the telnet remotely. Sorry, I’m not very versed in telnet usage.

Here’s the response from telnet to smtp.office365.com:

Trying 132.245.81.178...
Connected to outlook-namsouth.office365.com.
Escape character is '^]'.
220 BY2PR06CA037.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 12 Oct 2014 00:38:36 +0000

And here is the response from my host server:

Trying 192.254.235.140...
Connected to appinc.co.
Escape character is '^]'.
220-gator3253.hostgator.com ESMTP Exim 4.82 #2 Sat, 11 Oct 2014 19:39:34 -0500 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.

Thanks in advanced for any extra help you can provide.

@drmad

Perhaps office365.com is blocking your hostgator server. The fastest (and easiest) way is creating a support ticket in hostgator for they to check that conectivity issue.

@Synchro

You say you have no terminal access to your server, so I’m guessing those examples you posted are done from somewhere else so the fact that you can get to them doesn’t mean that you can from your hostgator server. It could also be a DNS issue on your hostgator config.

@ndub

Thanks for the advice @Synchro. I contacted HG, again, and asked them about the DNS and could there be anything in the firewall that would be blocking outbound traffic. Sure enough, it was the firewall. They made a change and now it works great! Really appreciate your and @drmad’s help!

@Synchro

Glad you sorted that out.

@ndub

@hargrig

Thanks bro..i will try that ))

@sahilsunesara

@ndub did u get solution regarding above matter?

@mrshiru

@Synchro

Read the guide the error links to.

@vaishnavhiren1993

hello @ndub , @drmad , @Synchro @hargrig ,
##ERROR is
SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

is is work on local host but once i run on godaddy server i can’t and error shot . plz solve and sugges me

**## My Code HTML is **«<style type=»text/css»>
/form/

label {
    position: absolute;
    -webkit-transform: translateY(6px);
    transform: translateY(6px);
    left: 13px;
    color: rgb(0, 0, 0);
    -webkit-transition: all 0.25s ease;
    transition: all 0.25s ease;
    pointer-events: none;
    font-size: 19px;
    font-family: "century gothic";
}

label .req {
    margin: 2px;
    color: #b11a1a;
}

label.active {
    -webkit-transform: translateY(50px);
    transform: translateY(50px);
    left: 2px;
    font-size: 14px;
    top: -70px;
}

label.active .req {
    opacity: 1;
}

label.highlight {
    color: #000;
}

input,
textarea {
    font-size: 17px;
    display: block;
    width: 100%;
    padding: 5px 10px;
    background: none;
    background-image: none;
    border: 1px solid #a0b3b0;
    color: #000;
    border-radius: 0;
    -webkit-transition: border-color .25s ease, box-shadow .25s ease;
    transition: border-color .25s ease, box-shadow .25s ease;
}

input:focus,
textarea:focus {
    outline: 0;
    border-color: #b11a1a;
}

textarea {
    border: 2px solid #a0b3b0;
    resize: vertical;
    height: 120px;
}

.field-wrap {
    position: relative;
    margin-bottom: 30px;
}

.top-row:after {
    content: "";
    display: table;
    clear: both;
}

.top-row>div {
    float: left;
    width: 48%;
    margin-right: 4%;
}

.top-row>div:last-child {
    margin: 0;
}

.button {
    border: 0;
    outline: none;
    border-radius: 0;
    padding: 10px 0;
    font-size: 1rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .1em;
    background: #b11a1a;
    color: #ffffff;
    -webkit-transition: all 0.5s ease;
    transition: all 0.5s ease;
    -webkit-appearance: none;
}

.button:hover,
.button:focus {
    background: #9b1717;
}

.button-block {
    display: block;
    width: 100%;
}

.forgot {
    margin-top: -20px;
    text-align: right;
}

.content {
    padding-bottom: 0px;
}

* {
    font-family: 'Century Gothic' !important;
}

</style>

Register and get more information

<br>
<form action="mail.php" method="POST" id="form">
    <input type="email" id="email" name="owneremail" maxlength="30" placeholder="enter email address to get user data " style="margin-bottom:30px">
    <div class="top-row">
        <div class="field-wrap">
            <label class="active highlight">
            First Name<span class="req">*</span>
          </label>
            <input type="text" pattern="[a-zA-Z]{3,30}" id="fname" name="fname" maxlength="30">
        </div>

        <div class="field-wrap">
            <label class="active highlight">
            Last Name<span class="req">*</span>
          </label>
            <input type="text" pattern="[a-zA-Z]{3,30}" id="lname" name="lname" maxlength="30">
        </div>
    </div>

    <div class="field-wrap">
        <label class="active highlight">
          Company name<span class="req">*</span>
        </label>
        <input type="text" class="half" name="company" id="cname">
    </div>

    <div class="top-row">
        <div class="field-wrap">
            <label class="active highlight">
            City<span class="req">*</span>
          </label>
            <input type="text" pattern="[a-zA-Z]{3,20}" name="city" id="city">
        </div>

        <div class="field-wrap">
            <label class="active highlight">
            State/Province<span class="req">*</span>
          </label>
            <input type="text" pattern="[a-zA-Z]{3,20}" name="state" id="province">
        </div>
    </div>

    <div class="top-row">
        <div class="field-wrap">
            <label class="active highlight">
             Email Address<span class="req">*</span>
           </label>
            <input type="email" required="required" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,3}$" name="email" id="memail">
        </div>

        <div class="field-wrap">
            <label class="active highlight">
            Phone<span class="req">*</span>
          </label>
            <input type="number" autocomplete="on" pattern="[0-9]{10,15}" name="phone" id="mobile">
        </div>
    </div>

    <div class="field-wrap">
        <label class="active highlight">
          Website<span class="req">*</span>
        </label>
        <input type="text" class="half" pattern="[a-zA-Z._-%^]" name="website" id="website" value="">
    </div>

    <div class="field-wrap">
        <label class="active highlight">
          Queries
        </label>
        <textarea name="queries" autocomplete="off"></textarea>
    </div>
    <button type="submit" value="Submit" name="Submit" class="button button-block">Send</button>

</form>

My PHP Code is

/form/

SMTPDebug = 3;
$mail->Debugoutput = ‘html’;
$mail->isSMTP();
$mail->Host = ‘ssl://smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->Username = ‘useremailaddress@gmail.com’;
$mail->Password = ‘userpassword’;
$mail->SMTPSecure = ‘ssl’;
$mail->Port = 465;
$mail->isHTML(true);
$mail->From = ‘useremailaddress@gmail.com’;
$mail->FromName = ‘this is test’;
$mail->addAddress($email_to);
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = ‘ASK FOR A DEMO’;
$mail->Body = ‘hello’;
if(!$mail->send()) {
echo ‘Message could not be sent.’;
echo ‘Mailer Error: ‘ . $mail->ErrorInfo;
} else {
echo ‘Message has been sent’;
echo «<script>alert(‘».$email_to.» thank you!’);</script>»;
}
}
?>

hello

@Synchro

You see that link to the troubleshooting guide? Click it, and read what it says about GoDaddy.

@PHPMailer
PHPMailer

locked as resolved and limited conversation to collaborators

Jan 18, 2019

Sometimes users need to send outgoing e-mails with external SMTP servers. Most popular example is PHPMailer with its ability to use free SMTP servers, for example Gmail. Here is the short piece of code, that allows to do this:

<?php
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerException;

require './mail/PHPMailer-master/src/Exception.php';
require './PHPMailer-master/src/PHPMailer.php';
require './mail/PHPMailer-master/src/SMTP.php';

date_default_timezone_set('Etc/UTC');


$milo="this is a test";

/**
 * This example shows sending a message using PHP's mail() function.
 */

//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

$mail->SMTPDebug = 0;

//Set who the message is to be sent from
$mail->setFrom('example@gmail.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('support@example.com', 'John Doe');
//Set the subject line
$mail->Subject = 'PHPMailer test 2 smtp';

$mail->CharSet = 'UTF-8';
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication
$mail->Username = "example@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "yourpassword";

$mail->msgHTML($milo);
//Replace the plain text body with one created manually
$mail->AltBody = $milo;

//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

If you have a Directadmin server with csf firewall installed, most probably you will get this message on execution:

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

Let’s investigate what causes this error. First, we need to change debug level in our script to get all error messages:

$mail->SMTPDebug = 4;

Here is what we are getting:

2018-03-29 13:01:34 Connection: opening to smtp.gmail.com:587, timeout=300, options=array()
2018-03-29 13:01:37 Connection failed. Error #2: stream_socket_client(): unable to connect to smtp.gmail.com:587 (Connection refused) [SMTP.php line 326]
2018-03-29 13:01:37 SMTP ERROR: Failed to connect to server: Connection refused (111)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

So the problem seems to be with opening port 587 – the script is even unable to connect. Let’s verify if we have this port blocked by firewall:

[root@server ~]# dmesg | grep 587

Most probably our output will look like this:

Firewall: *TCP_OUT Blocked* IN= OUT=eth0 SRC=62.210.90.101 DST=66.102.1.109 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=57285 DF PROTO=TCP SPT=50856 DPT=587 WINDOW=14600 RES=0x00 SYN URGP=0 UID=503 GID=505
Firewall: *TCP6OUT Blocked* IN= OUT=eth0 SRC=fe80:0000:0000:0000:fabc:12ff:fe48:effa DST=2a00:1450:400c:0c06:0000:0000:0000:006d LEN=80 TC=0 HOPLIMIT=64 FLOWLBL=0 PROTO=TCP SPT=45138 DPT=587 WINDOW=14400 RES=0x00 SYN URGP=0 UID=503 GID=505
Firewall: *TCP6OUT Blocked* IN= OUT=eth0 SRC=fe80:0000:0000:0000:fabc:12ff:fe48:effa DST=2a00:1450:400c:0c06:0000:0000:0000:006d LEN=80 TC=0 HOPLIMIT=64 FLOWLBL=0 PROTO=TCP SPT=45138 DPT=587 WINDOW=14400 RES=0x00 SYN URGP=0 UID=503 GID=505

Here’s the catch – our firewall is blocking outgoing connection through this port. We need to unlock it to continue. Here is the solution.

Open /etc/csf/csf.conf in any editor

Scroll to this line:

SMTP_BLOCK = "1"

Change it to

SMTP_BLOCK = "0"

Restart csf:

csf -r

Now you should be able to use your script without any issues.

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

If you are setting up your WordPress site with a SMTP plugin or using the PHPMailer library for sending email from your web server, you might encounter this SMTP Error: Failed to connect to server: Connection refused (111)/Network is unreachable (101). It’s a common connectivity issue on most shared hosting servers. In this post I will explain the issue in detail and show you how you can resolve it to have a fully functional email/SMTP setup.

SMTP connection requires a port such 25/465/587. Based on the type of connection you may also need to use encryption (e.g. SSL/TLS) or no encryption at all. SSL encryption is mainly used on port 465 where as TLS on port 587. On some shared hosts port 465/587 are blocked by default. This makes it so PHPMailer/SMTP plugin can’t even connect to the remote SMTP server. Sometimes those ports are open but they don’t support encryption. This causes the email to be sent without any encryption which can be a security issue. Your remote SMTP host can also decide which connection to accept. For example: If you try to make a non-encrypted connection to a SMTP host like Gmail (smtp.gmail.com) it will refuse since the connection is not secure. In that case the error may look similar to this,

  • unable to connect to ssl://smtp.gmail.com:465 (connection refused)
  • unable to connect to smtp.gmail.com:587 (connection refused)

Connection refused (111)/Network is unreachable (101) error can also occur if your web host simply doesn’t allow external SMTP connection. This issue is common on a GoDaddy server where they only allow their own SMTP configuration. If you contact them regarding this issue they will assure you that port 465 and 587 are open (when they are not). If you get back to them again they will tell you to use their SMTP server host instead of your own. So if you are on a GoDaddy server and having this issue your SMTP configuration should look like the following:

  • SMTP Host/Server: relay-hosting.secureserver.net
  • Port: 25
  • SMTPAuth: false

This should allow you to send email from your web server without any interruption/error. If you come across a different solution for resolving this type of error feel free to share it in the comments.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Smtp error data not accepted smtp server error data end command failed detail
  • Smtp error could not connect to smtp host что это
  • Slui exe 0x2a 0x80041002 как исправить
  • Slrr editor выдает ошибку
  • Smtp error could not connect to smtp host перевод

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии