如何在Laravel 5.1中使用Gmail发送邮件?

问题描述 投票:61回答:13

我一次又一次地尝试测试从localhost发送电子邮件,但我仍然不能。我不知道该怎么办。我尝试搜索找到解决方案,但我找不到一个。我编辑了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' => '[email protected]', '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
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

它仍然产生这样的错误:enter image description here

php email gmail laravel-5.1
13个回答
116
投票

首先登录你的Gmail帐户并在My account > Sign In And Security > Sign In to google下启用two step verification,然后你可以生成app password,你可以在.env文件中使用该应用程序密码。

您的.env文件将看起来像这样

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

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


0
投票

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

同时确保你在设置部分允许不太安全的应用程序。有关其他信息,您可以按照如何here


0
投票

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

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=passowrd
MAIL_ENCRYPTION=ssl

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

我通过检查谷歌服务器的错误代码找到了这个,这是使用完全并引导我到this网页。


0
投票

你也可以尝试添加laravel这两行:

[email protected]
MAIL_FROM_NAME="[email protected]"

运行此命令后,刷新邮件缓存配置:

php artisan config:cache

-1
投票

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

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


17
投票

尝试使用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
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

14
投票

你所要做的就是在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

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

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


12
投票

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

mail.php文件夹下打开你的config然后填写这个选项:

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

.env项目下打开您的root文件。也可以按照上面的选项编辑此文件

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进行授权


7
投票

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


0
投票

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

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


0
投票

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


0
投票

我的问题是,由于某种原因,用户名/密码从邮件配置变为NULL。要在发送电子邮件之前检查一下,请检查以下代码:

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

如果您的用户名/密码已被取消,请设置为:

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

0
投票

在bluehost我无法重置密码;这个驱动程序工作:

MAIL_DRIVER=sendmail
© www.soinside.com 2019 - 2024. All rights reserved.