SMTP ERROR: Failed to connect to server: Connection refused (111) ERROR MESSAGE
Asked Answered
M

3

6

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 PHPMailer\PHPMailer\PHPMailer;
$m = new PHPMailer;
$m->isSMTP();
$m->SMTPAuth = true;
$m->SMTPDebug = 2;

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

$m->SetFrom('[email protected]');
$m->FromName = 'Pouya Vaghefi';
$m->addReplyTo('[email protected]','Pouya Vey');
$m->addAddress('[email protected]','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...

Morisco answered 19/4, 2018 at 10:8 Comment(4)
Phpmailer has debug options for a reason ... please consult the documentation of Phpmailer on how to tackle w/ these kind of errors.Clambake
Can you try this one ?Excerpta
I would walk through this text and update your question:github.com/PHPMailer/PHPMailer/wiki/Troubleshooting This is most likely a DNS or Firewall issue like others have stated. Or your installation is missing a library.Gladygladys
Debugging is best solution to find your problem github.com/PHPMailer/PHPMailer/wiki/SMTP-DebuggingCotangent
T
7

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

Thyroxine answered 23/4, 2018 at 13:31 Comment(0)
I
3

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" <[email protected]>
2018-04-28 13:53:13     CLIENT -> SERVER: From: feiffy <[email protected]>
2018-04-28 13:53:13     CLIENT -> SERVER: Reply-To: "feiffy" <[email protected]>
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.

Irreligion answered 28/4, 2018 at 14:7 Comment(0)
S
-1

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?

Segregationist answered 25/4, 2018 at 21:35 Comment(1)
SparkPost is better than Mailgun :)Cotangent

© 2022 - 2024 — McMap. All rights reserved.