问题
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 = '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...
回答1:
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
回答2:
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.
回答3:
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?
来源:https://stackoverflow.com/questions/49918358/smtp-error-failed-to-connect-to-server-connection-refused-111-error-message