php - Swift_TransportException 无法使用 smtp.gmail.com stream_socket_client() 建立连接:无法连接到 tls://smtp.gmail.com:587
问题描述
最近我一直在做一个laravel
项目,该项目具有通过 gmail 的 smtp 发送电子邮件的功能。到目前为止,本地开发服务器很好,但生产服务器出现问题。错误:
Swift_TransportException
Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 (Connection refused)
注意:我正在使用与cpanel version 98.0.8
. 我还打开了不太安全的应用程序。
这是我的.env
文件
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=my_username@gmail.com
MAIL_PASSWORD="My_password"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=my_username@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
再次:本地开发服务器工作正常。
什么可能是一个好的解决方案?谢谢..
解决方案
步骤1:
将mail.php
项目中的内部配置文件夹替换为:
<?php
return [
/*
|--------------------------------------------------------------------------
| Mail Driver
|--------------------------------------------------------------------------
|
| Laravel supports both SMTP and PHP's "mail" function as drivers for the
| sending of e-mail. You may specify which one you're using throughout
| your application here. By default, Laravel is setup for SMTP mail.
|
| Supported: "smtp", "sendmail", "mailgun", "mandrill", "ses",
| "sparkpost", "postmark", "log", "array"
|
*/
'driver' => env('MAIL_DRIVER', 'smtp'),
/*
|--------------------------------------------------------------------------
| SMTP Host Address
|--------------------------------------------------------------------------
|
| Here you may provide the host address of the SMTP server used by your
| applications. A default option is provided that is compatible with
| the Mailgun mail service which will provide reliable deliveries.
|
*/
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
/*
|--------------------------------------------------------------------------
| SMTP Host Port
|--------------------------------------------------------------------------
|
| This is the SMTP port used by your application to deliver e-mails to
| users of the application. Like the host we have set this value to
| stay compatible with the Mailgun e-mail application by default.
|
*/
'port' => env('MAIL_PORT', 587),
/*
|--------------------------------------------------------------------------
| Global "From" Address
|--------------------------------------------------------------------------
|
| You may wish for all e-mails sent by your application to be sent from
| the same address. Here, you may specify a name and address that is
| used globally for all e-mails that are sent by your application.
|
*/
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
/*
|--------------------------------------------------------------------------
| E-Mail Encryption Protocol
|--------------------------------------------------------------------------
|
| Here you may specify the encryption protocol that should be used when
| the application send e-mail messages. A sensible default using the
| transport layer security protocol should provide great security.
|
*/
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
/*
|--------------------------------------------------------------------------
| SMTP Server Username
|--------------------------------------------------------------------------
|
| If your SMTP server requires a username for authentication, you should
| set it here. This will get used to authenticate with your server on
| connection. You may also set the "password" value below this one.
|
*/
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
/*
|--------------------------------------------------------------------------
| Sendmail System Path
|--------------------------------------------------------------------------
|
| When using the "sendmail" driver to send e-mails, we will need to know
| the path to where Sendmail lives on this server. A default path has
| been provided here, which will work well on most of your systems.
|
*/
'sendmail' => '/usr/sbin/sendmail -bs',
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings
|--------------------------------------------------------------------------
|
| If you are using Markdown based email rendering, you may configure your
| theme and component paths here, allowing you to customize the design
| of the emails. Or, you may simply stick with the Laravel defaults!
|
*/
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
/*
|--------------------------------------------------------------------------
| Log Channel
|--------------------------------------------------------------------------
|
| If you are using the "log" driver, you may specify the logging channel
| if you prefer to keep mail messages separate from other log entries
| for simpler reading. Otherwise, the default channel will be used.
|
*/
'log_channel' => env('MAIL_LOG_CHANNEL'),
];
第2步:
在您的 gmail 中:
- 去管理你的谷歌账户
- 里面去安全
- 使使用您的手机登录为关闭
- 关闭两步验证_
- 降低安全性应用访问
第 3 步:
登录到 chrome,然后在新选项卡中打开以下链接:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
在此按下继续按钮。
请确保使用上面的 gmail 链接从您的 gmail 帐户启用此 recaptcha。
第4步:
在您的项目中运行以下命令之后
php artisan optimize:clear
不要忘记启用 recaptacha。 如果没有 recaptacha,请尝试 2 到 3 次。
在此之后,您的电子邮件将被成功发送。
推荐阅读
- php - 使用 laravel 进行多文件上传和位置存储
- python - RASA X-Linux 服务器安装
- javascript - 为什么 package-lock.json 列出的依赖项与 package.json 不同?
- image - 将 svg 转换为像这样的可复制/可粘贴表情符号
- javascript - 如何对对象的子数组进行排序
- c++ - 数组指针 c++
- apache-kafka - 如何使用基于密钥的身份验证在 kafka 中使用 SFTP CSV SOURCE 连接器?
- flutter - 在 Flutter 中显示本地应用目录中的图像
- c# - 在视频的第三秒有图像
- node.js - Cloud Function + Cloud Build API + Node 10 的步骤?