当我尝试使用GMail SMTP通过Laravel发送电子邮件时,我遇到以下错误:
Swift_TransportException
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
这是错误的痕迹:
...
}
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));
if (false === $this->_stream) {
throw new Swift_TransportException(
'Connection could not be established with host ' . $this->_params['host'] .
' [' . $errstr . ' #' . $errno . ']'...
这是我的邮件配置:
'driver' => 'smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'from' => array('address' => '[email protected]', 'name' => 'some'),
'encryption' => 'tls',
'username' => '[email protected]',
'password' => 'mypassword',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false
我使用共享主机,localhost上的端口587已打开。
我有同样的问题,我以这种方式解决了它:
'driver' => 'sendmail',
您只需要更改该行。
我使用Swiftmailer遇到了同样的问题
无论如何,你不应该使用的快速脏黑客将编辑swiftmailer \ swiftmailer \ lib \ classes \ Swift \ Transport \ StreamBuffer.php。在_establishSocketConnection()第253行替换:
$options = array();
用这样的东西:
$options = array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false));
这将改变stream_context_create()的ssl选项($ options下面几行):
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno,
$errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));
这个You can find it here有一个肮脏的黑客
我的ENV也设置为
MAIL_DRIVER=smtp
MAIL_HOST=mail.mydomain.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
在终端中使用此命令
sudo ufw允许在“Postfix Submission”中启用SMTP端口587
您需要在Google帐户中创建2因素身份验证和自定义密码。另外,不要忘记为您正在使用的每个新主机添加自定义密码。
对于临时修复,您可以通过将env文件更新为'driver'=>'sendmail'来解决问题
检查可用磁盘空间。在我的情况下,我100%使用。
经过大量研究后,我发现这个有用。
https://www.google.com/settings/security/lesssecureapps。
打开上面的链接。
单击“启用”。并保存它。
然后尝试再次发送电子邮件。
对我来说它有效。
问题是smtp.gmail.com正在解析IPv6地址,而Google服务只侦听IPv4。您需要做的是设置源IP以确保域解析为IPv4而不是IPv6。
重要的方法:
->setSourceIp('0.0.0.0')
如何在代码中使用它:
$this->_transport = Swift_SmtpTransport::newInstance
(
'smtp.gmail.com',
465,
'ssl'
)
->setUsername('username')
->setSourceIp('0.0.0.0')
->setPassword('password');
除了加密和端口之外,我可以使用相同的设置。改成:
'encryption' => ssl,
'port' => 465,
由于这仅适用于localhost加密线,因此也应该是特定于环境的。所以相反,我做了以下:
env('MAIL_ENCRYPTION','tls'),
现在你可以在.env文件中设置它,这是特定于环境的,应该是.gitignore
我使用laravel forge + digitalocean遇到了同样的问题。
当我尝试telnet smtp.gmail.com 465时,我发现
telnet smtp.gmail.com 465
Trying 2404:6800:4003:c00::6d... # more than 30 sec
Trying 74.125.200.108... # less 1 sec
Connected to smtp.gmail.com.
可能是它连接超时的IPv6。
所以,我改变了gai.conf以优先ipv4而不是ipv6
我们/etc/gai.conf
#For sites which prefer IPv4 connections change the last line to
precedence ::ffff:0:0/96 100
...
# For sites which use site-local IPv4 addresses behind NAT there is
# the problem that even if IPv4 addresses are preferred they do not
# have the same scope and are therefore not sorted first. To change
# this use only these rules:
#
scopev4 ::ffff:169.254.0.0/112 2
scopev4 ::ffff:127.0.0.0/104 2
scopev4 ::ffff:0.0.0.0/96 14
通过更改我的.env文件解决了我的问题,如下所示:
'driver' => 'sendmail',
尝试
'encryption' => 'ssl',
'port' => 465,
如果是非Google Apps帐户,请务必按照建议启用安全性较低的应用。如果你不这样做,它将无法正常工作。
如果它是Google Apps帐户(即它是商家帐户),则会有一个管理面板来管理访问权限。您需要确保它仅通过身份验证而不是IP来指定访问权限,因为如果是IP,则您的IP可能不在列表中。
最后要尝试的是使用smtp.gmail.com的IPv4地址代替代码中的域名。我发现我的域不会使用域连接(因为它解析为IPv6地址),但是当我在其位置使用原始IP时会连接。
该错误可能是由于启用了2步验证。在这种情况下,您需要创建gmail应用程序密码并将其用作密码。