无法与主机 smtp.gmail.com [连接被拒绝 #111] 在 Laravel 5.6 电子邮件发送中建立连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52092789/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:01:03  来源:igfitidea点击:

Connection could not be established with host smtp.gmail.com [Connection refused #111] in laravel 5.6 email send

phplaravelsmtpgmaillaravel-5.6

提问by Amanullah Aman

Email Sending failed from cpnel but it works fine in my localhost.

从 cpnel 发送电子邮件失败,但它在我的本地主机中工作正常。

Here is my email configuration in .envfile.

这是我在.env文件中的电子邮件配置。

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

It works fine in localhost but not working in cpanel. In cpanel it shows

它在 localhost 中工作正常,但在 cpanel 中不起作用。在cpanel它显示

Connection could not be established with host smtp.gmail.com [Connection refused #111]

How do i solve this?

我该如何解决这个问题?

回答by Amanullah Aman

Problem Solved.I made the change two things here MAIL_DRIVERand MAIL_PORT.

问题解决了。我在这里做了两件事MAIL_DRIVERMAIL_PORT

MAIL_DRIVER=sendmail
MAIL_PORT=587

So, my full code is:

所以,我的完整代码是:

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

It works fine

它工作正常

回答by Neek

I know this question has been answered but I have an alternative solution that matches the original description and might help someone. You did not answer Loek's question about whether you run cPanel or not. We run cPanel with lfd/csf firewall, and it has TCP_OUT configuration that lists what outbound ports may be connected to. Port 465 (tls) was not in that list, so we got 'Connection Refused' reports, even trying a low level telnet test on the command line of our VPS server:

我知道这个问题已经得到回答,但我有一个与原始描述相匹配的替代解决方案,可能会对某人有所帮助。您没有回答 Loek 关于您是否运行 cPanel 的问题。我们使用 lfd/csf 防火墙运行 cPanel,它有 TCP_OUT 配置,列出了哪些出站端口可以连接到。端口 465 (tls) 不在该列表中,因此我们收到了“连接被拒绝”报告,甚至在我们的 VPS 服务器的命令行上尝试进行低级别 telnet 测试:

$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
telnet: connect to address 74.125.140.109: Connection refused
Trying 74.125.140.108...
telnet: connect to address 74.125.140.108: Connection refused
Trying 2a00:1450:400c:c08::6d...
telnet: connect to address 2a00:1450:400c:c08::6d: Connection refused

The same telnet test worked fine from my local machine.

相同的 telnet 测试在我的本地机器上运行良好。

After adding 465 to the TCP_OUT list:

将 465 添加到 TCP_OUT 列表后:

  • Load WHM control panel
  • go to ConfigServer Security & Firewall
  • go to Firewall Configuration
  • find TCP_OUTand include 465 (or whatever port you are trying to connect to) to the list
  • click Changeat the bottom of the page, then Restart lfd/csf.
  • 加载 WHM 控制面板
  • ConfigServer Security & Firewall
  • Firewall Configuration
  • 查找TCP_OUT并将 465(或您尝试连接的任何端口)包含到列表中
  • 单击Change页面底部的 ,然后单击Restart lfd/csf

Telnet now succeeds, as does smtpauth mail sending via PHPMailer:

Telnet 现在成功了,通过 PHPMailer 发送 smtpauth 邮件也是如此:

$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
Connected to smtp.gmail.com.
Escape character is '^]'.

(this indicates the socket connection was established.. you probably don't want to actually talk SMTP to Google so hit ctrl-] and type quitand hit enter to close the connection)

(这表明套接字连接已建立。您可能不想实际将 SMTP 与 Google 对话,因此请按 ctrl-] 并键入quit并按 Enter 以关闭连接)

Of course this isn't cPanel specific, you may be running another firewall manager that is not allowing outbound connections on the port you're trying to connect to.

当然,这不是特定于 cPanel 的,您可能正在运行另一个防火墙管理器,该管理器不允许在您尝试连接的端口上进行出站连接。

回答by Amanullah Aman

I did face this issue before, I did allow less secure apps from google account manager. And my problem was solved.

我之前确实遇到过这个问题,我确实允许来自谷歌客户经理的安全性较低的应用程序。我的问题解决了。

It seems like that you too need authorization for extension application in order to use gmail.

似乎您也需要扩展应用程序的授权才能使用 gmail。

You can follow given below steps...

您可以按照以下步骤...

Sign-in into your gmail account.

登录您的 Gmail 帐户。

Access this linkto change settings.

访问此链接以更改设置。

Then you have to turn on Allow less secure apps.

然后你必须打开Allow less secure apps

After that try again to send email through you host.

之后再次尝试通过您的主机发送电子邮件。

回答by zafar Khan

After couple of days research. I was having issue of Connection could not be established with host smtp.gmail.com [Connection refused ] because of my godaddy server

经过几天的研究。我遇到了无法与主机 smtp.gmail.com 建立连接的问题 [连接被拒绝] 因为我的 Godaddy 服务器

As GoDaddy blocks SMTP communication over ports 465 and 587 and possibly 25 originating from your site. They will allow mail flow only through their own SMTP servers.

由于 GoDaddy 会阻止通过端口 465 和 587 以及可能来自您站点的 25 的 SMTP 通信。他们将只允许邮件流通过他们自己的 SMTP 服务器。

That was issue in my case. hope it helps someone

这就是我的问题。希望它可以帮助某人

For reference check this: https://pk.godaddy.com/community/Using-WordPress/Connection-Refused-for-GMail-SMTP/td-p/33107

作为参考,请检查:https: //pk.godaddy.com/community/Using-WordPress/Connection-Refused-for-GMail-SMTP/td-p/33107