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


76

我一次又一次尝试测试从本地主机发送电子邮件,但是我仍然不能。我不知道该怎么做。我尝试搜索以找到解决方案,但找不到。我编辑了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帐户启用了两步验证?
2015年

对不起,我不知道。所以我们必须先在我的Gmail帐户中启用它?
SRENG Khorn 2015年

@SRENGKhorn iam也遇到相同的错误。您有解决方案吗?
prudhvi259

@与邮件枪prudhvi259测试mailgun.com
SRENG Khorn

Answers:


138

首先登录到您的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
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

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


在遵循您的建议后,发生了以下情况:无法与主机smtp.gmail.com建立连接[试图以其访问权限禁止的方式访问套接字。#10013]
SRENG Khorn 2015年

1
您使用的是应用专用密码还是自己的Gmail密码?或者如果您有任何互联网安全软件(如norton, kaspersky等),请禁用它并测试发送电子邮件是否有效。
2015年

是的,我使用的是Google的应用密码,并且我禁用了McAfee,但没有任何反应。
SRENG Khorn 2015年

2
我对此有解决方案。我希望它能起作用。还需要吗 让我知道。
威廉·弗朗西斯·戈麦斯

1
不要忘了让Gmail发送电子邮件德外google.com/settings/security/lesssecureapps
法里斯Rayhan的


16

您所要做的只是在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=zh_CN

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


13

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

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

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

.envroot项目下打开文件。也可以按照上述选项编辑该文件,例如

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帐户登录以授权不受信任的连接。访问此链接 以授权


7

如果您在正确设置所有配置后仍然能够发送邮件并得到禁止或超时错误,则可以allow less secure apps to access your account在gmail中进行设置。您可以按照此处的操作方法进行操作


是的,那是最好的解决方案。我具有以前的所有设置,但是突然我无法登录了。允许安全性较低的应用访问该帐户对我来说是有效的。
彼得(Peter

我收到的535-5.7.8用户名和密码不被接受,这解决了我的问题
Mauricio Trajano

3

注意:Laravel 7用MAIL_MAILER取代了MAIL_DRIVER

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

1

您需要先启用Google帐户的应用密码->安全部分链接

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

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


0

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


0

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

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

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

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


0

如果您使用的是电子邮件密码,则应将其替换为应用程序密码。要设置APP密码,需要先启用两步身份验证,然后再设置密码,以后可以禁用该密码。

另外,请确保您在设置部分允许使用的安全性较低的应用。有关其他信息,请按照此处的操作方法进行操作。


0

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

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环境变量。

我通过检查来自google服务器的错误代码找到了有关此问题的信息,该代码已被用完,并引导我进入网页。


0

您也可以尝试向laravel添加以下两行:

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

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

php artisan config:cache

0

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安全设置中访问您的帐户。


0

使用G Suite帐户

如果您使用的是G Suite帐户,则任何尝试都会失败。至少在那时,正在输入答案。您必须使用@gmail.com帐户,否则其他任何操作都@example.com将无法使用。

使用gmail.com地址后。您只需要更新,.env就像大多数人已经提到的那样。

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=****@gmail.com
MAIL_PASSWORD=16digitapppassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=****@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

不要忘记创建一个App密码,如果您没有看到该选项,则可能未启用2要素身份验证。如果您采用这种方法,则无需允许安全性较低的应用程序。


-1

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

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.