SMTP错误:无法连接到服务器:拒绝(111)与Office365的连接

2022-03-31 00:00:00 php phpmailer

很抱歉,如果这是一条人流很多的路。我看过其他关于这个问题的帖子,但它们既没有解决我的问题,也没有点燃一个灯泡来帮助我自己解决这个问题。

以下是我的代码:

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();
}

我已确认域、用户名和密码都是正确的,并且传入正确。重要的是要注意,在启动之前,这在我们的本地开发服务器上有效。一旦网站被转移到我们的托管账户(Hostgator),就是它停止工作的时候。我已向HG确认我们的服务器上的端口587已打开。

以下是我看到的错误消息:

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.

非常感谢您能提供的任何帮助,即使它只是一篇文章的链接,该文章解释了为什么它现在在我们的生产环境中不起作用。


解决方案

答案对我都不起作用。 几个小时后,我发现了问题,但只适用于Cpanel/WHM

  • 登录到WHM。
  • 转到ConfigServer Security Inside Firewall Inside插件选项(&A)。
  • 点击防火墙配置
  • 按SMTP设置筛选
  • 查找SMTP_ALLOWUSER选项并添加以COM分隔的Cpanel帐户的用户名
  • 重新启动防火墙。

如果您无权访问WHM,请咨询您的提供商。

相关文章