简体   繁体   English

无法使用 smtp.gmail.com 建立 Swift_TransportException 连接 stream_socket_client():无法连接到 tls://smtp.gmail.com:587

[英]Swift_TransportException Connection could not be established with smtp.gmail.com stream_socket_client(): unable to connect to tls://smtp.gmail.com:587

Recently I have been working on a laravel project that has functionality of sending email through smtp of gmail. So far so good with local development server but the problem arises with production server.最近我一直在研究一个laravel项目,该项目具有通过 gmail 的 smtp 发送 email 的功能。到目前为止,本地开发服务器一直很好,但生产服务器出现了问题。 Error:错误:

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) 

Note : I am using shared hosting with cpanel version 98.0.8 .注意:我使用共享主机与cpanel version 98.0.8 I also turned on less secure app .我还打开了不太安全的应用程序

Here is my .env file这是我的.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}"

Again: Local development server is working properly.再次:本地开发服务器工作正常。

What could be a possible good solution?什么可能是一个好的解决方案? Thank you..谢谢..

Step 1:第1步:

Replace your mail.php inside config folder in your project as:将项目中配置文件夹中的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'),

];

Step 2:第2步:

In your gmail:在您的 gmail 中:

  1. Go to manage your google account Go 管理您的谷歌账户
  2. Inside that go to security里面那个 go 到安全
  3. Make Use your phone to sign in as off关闭使用手机登录
  4. Make 2-Step Verification as off关闭两步验证
  5. Make Less secure app access as on降低安全应用程序访问权限

Step 3:第 3 步:

Login to chrome and then in new tab open below link:登录到 chrome,然后在新标签页中打开以下链接:

https://accounts.google.com/b/0/DisplayUnlockCaptcha https://accounts.google.com/b/0/DisplayUnlockCaptcha

In this make press continue button.在此制作中按继续按钮。

Please make sure to make this recaptcha enable from you gmail account using above gmail link.请确保使用上面的 gmail 链接从您的 gmail 帐户启用此重新验证。

Step 4:第四步:

After this run below command inside your project在你的项目中运行下面的命令之后

php artisan optimize:clear

Don't forget to make recaptacha enable.不要忘记启用 recaptacha。 Try 2 to 3 times if if recaptacha is not unabled.如果无法重新验证,请尝试 2 到 3 次。

After this your email will be sent successfully.在此之后,您的 email 将成功发送。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP Fatal error: Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com:stream_socket_client() - PHP Fatal error: Uncaught Swift_TransportException: Connection could not be established with host ssl://smtp.gmail.com :stream_socket_client() Connection could not be established with host smtp.gmail.com:stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 - Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to ssl://smtp.gmail.com:465 Swift_TransportException无法与主机smtp.gmail.com建立连接[连接超时#110] - Swift_TransportException Connection could not be established with host smtp.gmail.com [Connection timed out #110] “无法在本地主机上与主机 smtp.gmail.com [#0] 建立 Swift_TransportException 连接” - “Swift_TransportException Connection could not be established with host smtp.gmail.com [ #0]” on localhost Laravel:Swift_TransportException无法与主机smtp.gmail.com建立连接 - Laravel : Swift_TransportException Connection could not be established with host smtp.gmail.com Swift_TransportException 无法与主机 smtp.gmail.com 建立连接 - Swift_TransportException Connection could not be established with host smtp.gmail.com Laravel 5.4:无法通过主机smtp.gmail.com建立Swift_TransportException连接 - Laravel 5.4: Swift_TransportException connection could not be established with host smtp.gmail.com Getting "Connection could not be established with host smtp.gmail.com:stream_socket_client()" error in laravel 8 on localhost for sending email - Getting "Connection could not be established with host smtp.gmail.com :stream_socket_client()" error in laravel 8 on localhost for sending email 未捕获的异常“Swift_TransportException”带有消息“无法与主机 smtp.gmail.com 建立连接 [连接超时 #110]” - Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.gmail.com [Connection timed out #110]' 如何在实时服务器上的 Laravel 5.4 中发送电子邮件 Swift_TransportException 无法与主机 smtp.gmail.com 建立连接 - how to send email in laravel 5.4 on live server Swift_TransportException Connection could not be established with host smtp.gmail.com
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM