【问题标题】:How to to send mail using gmail in Laravel?如何在 Laravel 中使用 gmail 发送邮件?
【发布时间】:2015-12-07 12:42:37
【问题描述】:

我一次又一次地尝试测试从本地主机发送电子邮件,但我仍然不能。我已经不知道该怎么做了。我尝试搜索以找到解决方案,但找不到。我编辑了 config/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", "mail", "sendmail", "mailgun", "mandrill", "ses", "log"
    |
    */

    '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.gmail.com'),

    /*
    |--------------------------------------------------------------------------
    | 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' => 'myemail@gmail.com', 'name' => 'Do not Reply'],

    /*
    |--------------------------------------------------------------------------
    | 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'),

    /*
    |--------------------------------------------------------------------------
    | SMTP Server Password
    |--------------------------------------------------------------------------
    |
    | Here you may set the password required by your SMTP server to send out
    | messages from your application. This will be given to the server on
    | connection so that the application will be able to send messages.
    |
    */

    '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',

    /*
    |--------------------------------------------------------------------------
    | Mail "Pretend"
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, e-mail will not actually be sent over the
    | web and will instead be written to your application's logs files so
    | you may inspect the message. This is great for local development.
    |
    */

    'pretend' => false,

];
`

我已经像这样编辑了.env 文件:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

它仍然产生这样的错误:

【问题讨论】:

  • 您需要为此创建应用密码。您是否为您的 gmail 帐户启用了两步验证?
  • 对不起,我不知道。所以我们必须先在我的 gmail 帐户中启用?
  • @SRENGKhorn iam 也遇到了同样的错误。你有解决方案吗?
  • @prudhvi259 用邮筒测试它mailgun.com

标签: php laravel email gmail


【解决方案1】:

这些配置对我有用,你使用端口 25,465,587

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

【讨论】:

    【解决方案2】:

    注意:Laravel 7 将 MAIL_DRIVER 替换为 MAIL_MAILER

    MAIL_MAILER=smtp    
    MAIL_HOST=smtp.gmail.com   
    MAIL_PORT=587      
    MAIL_USERNAME=yourgmailaddress
    MAIL_PASSWORD=yourgmailpassword
    MAIL_ENCRYPTION=tls
    

    允许来自“Google 帐户”的安全性较低的应用 - https://myaccount.google.com/ - 设置 - 不太安全的应用访问(开启)

    刷新缓存配置:

    php artisan config:cache
    

    对于 Apache:

    sudo service apache2 restart
    

    【讨论】:

      【解决方案3】:

      您需要先启用您的谷歌帐户的应用密码 -> 安全部分link

      然后将生成的应用密码复制并粘贴到 .env 文件中

      MAIL_DRIVER=smtp
      MAIL_HOST=smtp.googlemail.com
      MAIL_PORT=465
      MAIL_USERNAME=account@gmail.com
      MAIL_PASSWORD=app_password
      MAIL_ENCRYPTION=ssl
      

      【讨论】:

        【解决方案4】:

        MAIL_DRIVER=smtp
        MAIL_HOST=smtp.gmail.com
        MAIL_PORT=587
        MAIL_USERNAME=yourmail@gmail.com
        MAIL_PASSWORD=yourpassword
        MAIL_ENCRYPTION=tls
        MAIL_FROM_NAME='Name'

        在 Gmail 安全设置中允许不太安全的应用访问您的帐户。

        【讨论】:

          【解决方案5】:

          您也可以尝试在 laravel 中添加这两行:

          MAIL_FROM_ADDRESS=you@email.com
          MAIL_FROM_NAME="you@email.com"
          

          在此之后运行此命令以刷新邮件缓存配置:

          php artisan config:cache
          

          【讨论】:

            【解决方案6】:

            尝试各种组合后为我工作。

            MAIL_DRIVER=smtp
            MAIL_HOST=smtp.gmail.com
            MAIL_PORT=465
            MAIL_USERNAME=mail@gmail.com
            MAIL_PASSWORD=passowrd
            MAIL_ENCRYPTION=ssl
            

            需要生成应用程序密码https://myaccount.google.com/security,并将其作为MAIL_PASSWORD环境变量。

            我通过检查来自谷歌服务器的错误代码发现了这一点,该代码已用完并引导我到this 网页。

            【讨论】:

              【解决方案7】:

              这是我尝试过的工作示例:

              config 文件夹下打开您的mail.php,然后填写此选项:

              'driver'     => env('MAIL_DRIVER', 'smtp'),
              'host'       => env('MAIL_HOST', 'smtp.gmail.com'),
              'port'       => env('MAIL_PORT', 587),
              'from'       => ['address' =>'youremail@mail.com', 'name' => 'Email_Subject'],
              'encryption' => env('MAIL_ENCRYPTION', 'tls'),
              'username'   => env('MAIL_USERNAME','yourusername@mail.com'),
              'password'   => env('MAIL_PASSWORD','youremailpassword'),
              'sendmail'   => '/usr/sbin/sendmail -bs',
              

              root 项目下打开您的.env 文件。还要在上面编辑此文件 选择这样的

              MAIL_DRIVER=smtp    
              MAIL_HOST=smtp.gmail.com   
              MAIL_PORT=587      
              MAIL_USERNAME=youremailusername
              MAIL_PASSWORD=youremailpassword
              MAIL_ENCRYPTION=tls
              

              然后通过运行此命令清除您的配置

              php artisan config:cache
              

              重启本地服务器

              首先尝试使用包含邮件功能的控制器访问您的路由 时间仍然错误Authentication Required。您需要通过以下方式登录 您的 gmail 帐户以授权不受信任的连接。访问此link 授权

              【讨论】:

                【解决方案8】:

                如果您使用的是邮箱密码,则应将其替换为应用密码。要设置应用密码,您需要在设置密码之前启用两步验证,以后可以禁用。

                还要确保您在设置部分允许安全性较低的应用程序。有关更多信息,您可以关注here

                【讨论】:

                  【解决方案9】:

                  只需将 MAIL_ENCRYPTION=null 更改为 MAIL_ENCRYPTION=tls

                  并运行此命令“php artisan config:cache”

                  【讨论】:

                    【解决方案10】:

                    在 bluehost 中我无法重置密码;使用此驱动程序:

                    MAIL_DRIVER=sendmail
                    

                    【讨论】:

                    • 在 cpanel 托管中使用此驱动器对我有用
                    【解决方案11】:

                    您所要做的就是在 you.env 文件中进行编辑,仅此而已。

                    MAIL_DRIVER=smtp
                    MAIL_HOST=smtp.gmail.com
                    MAIL_PORT=465
                    MAIL_USERNAME=<your_email_address>
                    MAIL_PASSWORD=<your_gmail_app_password_>
                    MAIL_ENCRYPTION=ssl
                    

                    对于应用程序密码转到 https://support.google.com/accounts/answer/185833?hl=en

                    并生成您的应用密码并保存以备将来使用。因为一旦您生成应用密码,您将无法重新编辑密码或更改相同的应用密码。(您可以创建多个应用密码)

                    【讨论】:

                    • ssl 和 tls 有什么不同?
                    • 我发现 ssl 将电子邮件直接发送到垃圾邮件但 tls 不是
                    【解决方案12】:

                    对我来说问题是由于某种原因用户名/密码来自邮件配置的 NULL。 要在发送电子邮件之前使用以下代码进行检查:

                    dd(Config::get('mail'));
                    

                    如果您的用户名/密码为空,则设置为:

                    Config::set('mail.username', 'yourusername');
                    Config::set('mail.password', 'yourpassword');
                    

                    【讨论】:

                      【解决方案13】:

                      如果您在 XAMPP 上进行开发,那么您将需要 SMTP 服务来发送电子邮件。尝试使用 MailGun 帐户。它免费且易于使用。

                      【讨论】:

                        【解决方案14】:

                        你的MAIL_PASSWORD=must a APPpasword 更改 .env 后停止服务器然后清除 configuratios cahce php artisan config:cahce 并再次启动服务器

                        参考 Cannot send message without a sender address in laravel 5.2 I have set .env and mail.php both

                        【讨论】:

                          【解决方案15】:

                          如果您在正确设置所有配置后仍然可以发送邮件并收到禁止或超时错误,您可以在 gmail 中设置 allow less secure apps to access your account。你可以关注如何here

                          【讨论】:

                          • 是的,这是最好的解决方案。我有所有以前的设置,但突然我无法登录了。允许不太安全的应用访问该帐户对我有用。
                          • 我收到了 535-5.7.8 的用户名和密码不被接受,这解决了我的问题
                          【解决方案16】:

                          尝试使用 sendmail 而不是 smtp 驱动程序(根据这些建议:http://code.tutsplus.com/tutorials/sending-emails-with-laravel-4-gmail--net-36105

                          MAIL_DRIVER=sendmail
                          MAIL_HOST=smtp.gmail.com
                          MAIL_PORT=587
                          MAIL_USERNAME=your@gmail.com
                          MAIL_PASSWORD=apppassword
                          MAIL_ENCRYPTION=tls
                          

                          【讨论】:

                            【解决方案17】:

                            首先登录你的gmail账号,在My account &gt; Sign In And Security &gt; Sign In to google下,启用two step verification,然后你可以生成app password,你可以在.env文件中使用该应用密码。

                            您的.env 文件将如下所示

                            MAIL_DRIVER=smtp
                            MAIL_HOST=smtp.gmail.com
                            MAIL_PORT=587
                            MAIL_USERNAME=myemail@gmail.com
                            MAIL_PASSWORD=apppassword
                            MAIL_ENCRYPTION=tls
                            

                            在对 .env 文件进行更改后,不要忘记运行 php artisan config:cache

                            【讨论】:

                            • 按照您的建议后发生了这种情况:无法与主机 smtp.gmail.com 建立连接 [试图以访问权限禁止的方式访问套接字。 #10013]
                            • 您使用的是应用密码还是您自己的gmail密码?或者如果您有任何互联网安全软件,如norton, kaspersky 等,请将其禁用并测试发送电子邮件是否有效。
                            • 是的,我使用的是谷歌的应用密码,我禁用了 McAfee,但没有任何反应。
                            • 我有解决方案。我希望它会奏效。你还需要吗?告诉我。
                            • 别忘了允许gmail在google.com/settings/security/lesssecureapps之外发送邮件
                            猜你喜欢
                            • 2020-12-16
                            • 2017-03-06
                            • 2019-05-30
                            • 2018-02-27
                            • 1970-01-01
                            • 1970-01-01
                            • 2016-08-31
                            • 1970-01-01
                            • 2017-01-16
                            相关资源
                            最近更新 更多