php SMTP 错误:无法连接到服务器:与 Office365 的连接被拒绝 (111)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26318878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SMTP ERROR: Failed to connect to server: Connection refused (111) with Office365
提问by Neal Williams
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']; //[email protected]
$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('[email protected]', 'Some Name');
$mail->addReplyTo('[email protected]', 'SenderFirst SenderLast');
$mail->addBCC('[email protected]');
$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.
我已经确认域、用户名和密码都是正确的并且被正确传递。重要的是要注意,这在启动之前在我们的本地开发服务器上有效。一旦网站被移动到我们的托管帐户 (Hostgator),它就会停止工作。我已与 HG 确认端口 587 在我们的服务器上已打开。
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.
非常感谢可以提供的任何帮助,即使它只是一篇文章的链接,解释了为什么它现在在我们的生产环境中不起作用。
回答by Juan Angel
None of the answers worked for me. After many hours, I found the problem, but only works for Cpanel/WHM
没有一个答案对我有用。几个小时后,我发现了问题,但仅适用于 Cpanel/WHM
- Login into WHM.
- Go to ConfigServer Security & Firewall inside plugins option.
- Click on Firewall configuration
- Filter by SMTP Settings
- Look for SMTP_ALLOWUSER option and add the Cpanel account's username separated by coma
- Restart the firewall.
- 登录 WHM。
- 转到插件选项中的 ConfigServer Security & Firewall。
- 点击防火墙配置
- 按 SMTP 设置过滤
- 查找 SMTP_ALLOWUSER 选项并添加以逗号分隔的 Cpanel 帐户的用户名
- 重启防火墙。
If you don't have access to WHM ask your provider.
如果您无法访问 WHM,请咨询您的提供商。
回答by vundek
In PHP 5.5 and phpmailer there's a bug with the port number. Don't set port number ( mail->port = ....) this causes the error: "smtp error failed to connect to server connection refused 111"
在 PHP 5.5 和 phpmailer 中,端口号存在错误。不要设置端口号(mail->port = ....)这会导致错误:“smtp error failed to connect to server connection denied 111”
Leave it at the default port number of 25 and it works !
将其保留在默认端口号 25 上,它可以正常工作!
回答by DigitalFlare
If you are using cPanel/WHM you need to make sure:
如果您使用的是 cPanel/WHM,您需要确保:
Restrict outgoing SMTP to root, exim, and mailman (FKA SMTP Tweak) - is set to OFF. (This can be edited inside "Server Configuration ? Tweak Settings" (Search: SMTP))
将传出 SMTP 限制为 root、exim 和 mailman (FKA SMTP Tweak) - 设置为 OFF。(这可以在“服务器配置?调整设置”(搜索:SMTP)中进行编辑)
If you also have the ConfigServer Security & Firewall enabled, you will need to edit your Firewall configuration. Click 'Firewall Configuation' then choose "Filter by SMTP Settings". Now look for SMTP_ALLOWUSER option and add the cPanel account's username separated by coma. Hit "Change" and then restart the firewall.
如果您还启用了 ConfigServer 安全和防火墙,则需要编辑防火墙配置。单击“防火墙配置”,然后选择“按 SMTP 设置过滤”。现在查找 SMTP_ALLOWUSER 选项并添加以逗号分隔的 cPanel 帐户的用户名。点击“更改”,然后重新启动防火墙。
回答by Neal Williams
It turns out that HG needed to modify the settings to the firewall on our server. Once they did that, it worked great. So, if you're having a similar problem, I'd recommend making sure everything is correct on your end, but then to check with your hosting provider to see what needs to be done on their end.
事实证明,HG 需要修改我们服务器上防火墙的设置。一旦他们这样做了,效果很好。因此,如果您遇到类似的问题,我建议您确保一切都正确,然后与您的托管服务提供商联系,看看他们需要做什么。