php 如何处理 Laravel 的 SMTP 驱动程序中的自签名 TLS 证书?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30714229/
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
How to deal with self-signed TLS certificates in Laravel's SMTP driver?
提问by Alan
I'm trying to send an email with this configuration:
我正在尝试使用此配置发送电子邮件:
return [
'driver' => 'smtp',
'host' => 'mail.mydomain.com',
'port' => 26,
'from' => ['address' => '[email protected]', 'name' => 'Mailer'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
When I submit the form I receive this erorr:
当我提交表单时,我收到此错误:
ErrorException in StreamBuffer.php line 95:
stream_socket_enable_crypto(): SSL operation failed with code 1.
OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
I found this solution where people seems to have solved the problem with the same library but I cant manage to solve it in Laravel.
我找到了这个解决方案,人们似乎用同一个库解决了这个问题,但我无法在 Laravel 中解决它。
采纳答案by geggleto
Well in that link you provided the solution is straight-forward.
那么在该链接中,您提供的解决方案很简单。
The correct solution is to fix your SSL config - it's not PHP's fault!
正确的解决方案是修复您的 SSL 配置 - 这不是 PHP 的错!
回答by M Arfan
Add this at bottom of your config/mail.php
在 config/mail.php 的底部添加这个
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
this will solve your problem.
这将解决您的问题。
Editor's note: disabling SSL verification has security implications.Without verification of the authenticity of SSL/HTTPS connections, a malicious attacker can impersonate a trusted endpoint (such as GitHub or some other remote Git host), and you'll be vulnerable to a Man-in-the-Middle Attack. Be sure you fully understand the security issues before using this as a solution.
编者注:禁用 SSL 验证具有安全隐患。如果不验证 SSL/HTTPS 连接的真实性,恶意攻击者可以冒充受信任的端点(例如 GitHub 或其他一些远程 Git 主机),您将容易受到中间人攻击。在使用它作为解决方案之前,请确保您完全了解安全问题。
回答by mosh442
In my case the problem was related to SSL. My SMTP has a self-signed certificate and my laravel was running on top of PHP 5.6 which disables the 'allow_self_signed' context variable to false and enables 'verify_peer' and hence poping the error when sending an email.
就我而言,问题与 SSL 有关。我的 SMTP 有一个自签名证书,我的 laravel 运行在 PHP 5.6 之上,它禁用了“allow_self_signed”上下文变量为 false 并启用“verify_peer”,因此在发送电子邮件时弹出错误。
Since I didn't wanted to hack around swiftmailer code I added the Certificate Authority (CA) file of my server as trusted CA for my system executing laravel.
由于我不想破解 swiftmailer 代码,因此我将服务器的证书颁发机构 (CA) 文件添加为我的系统执行 laravel 的可信 CA。
I did that getting the CA cert of my smtp server, something like
我这样做是为了获得我的 smtp 服务器的 CA 证书,例如
-----BEGIN CERTIFICATE-----
MIIElTCCA32gAwIBAgIJAMZjjNg64RQwMA0GCSqGSIb3DQEBCwUAMIGNMQswCQYD
VQQGEwJVUzEMMAoGA1UECBMDTi9BMQwwCgYDVQQHEwNOL0ExJDAiBgNVBAoTG1pp
...
5a8a4QEwWmnAOgHetsOCvhfeGW3yAJPD8Q==
-----END CERTIFICATE-----
and write it in my laravel machine which has an ubuntu 14.04 to a file named /usr/local/share/ca-certificates/my_cert.crt
. It is crucial to end the file with .crt
and also make it readable for everyone.
并将它写在我的 Laravel 机器上,它有一个 ubuntu 14.04 到一个名为/usr/local/share/ca-certificates/my_cert.crt
. 结束文件.crt
并使其对每个人都可读是至关重要的。
Then call update-ca-certificates
and the certificate will be added to the list of valid CAs of your server.
然后调用update-ca-certificates
,证书将被添加到您服务器的有效 CA 列表中。
回答by Maciej Laskowski
In case you are using Laravel 7.0 you can disable SSL verification in SwiftMailer this way (please note that disabling SSL verification is not recommended!):
如果您使用的是 Laravel 7.0,您可以通过这种方式在 SwiftMailer 中禁用 SSL 验证(请注意,不建议禁用 SSL 验证!):
config/mail.php
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
],
],
],