php 如何在 Laravel 中使用 gmail 发送邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32515245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to to send mail using gmail in Laravel?
提问by SRENG Khorn
I try again and again to test sending an email from localhost but I still cannot. I don't know anymore how to do it. I try search to find solution but I cannot find one. I edited config/mail.php:
我一次又一次地尝试测试从本地主机发送电子邮件,但我仍然不能。我不知道该怎么做了。我尝试搜索以找到解决方案,但找不到。我编辑了 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,
];
`
and I edited .env
file like this already:
我.env
已经编辑了这样的文件:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null
回答by Sid
first login to your gmail account and under My account > Sign In And Security > Sign In to google
, enable two step verification
, then you can generate app password
, and you can use that app password in .env
file.
首先登录到您的 Gmail 帐户并在 下My account > Sign In And Security > Sign In to google
启用two step verification
,然后您可以生成app password
,并且您可以在.env
文件中使用该应用程序密码。
Your .env
file will then look something like this
您的.env
文件将如下所示
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls
Don't forget to run php artisan config:cache
after you make changes in your .env
file.
php artisan config:cache
更改.env
文件后不要忘记运行。
回答by Martins
Try using sendmailinstead of smtpdriver (according to these recommendations: http://code.tutsplus.com/tutorials/sending-emails-with-laravel-4-gmail--net-36105)
尝试使用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
回答by Rahul Kulabhi
All you have to do is just edit in you.env file, that's it.
您所要做的就是在 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
for app password goto https://support.google.com/accounts/answer/185833?hl=en
应用密码转到 https://support.google.com/accounts/answer/185833?hl=en
and genearate your app pasword and save for future use. because once you generate app password you cannot re-edit password or change same app password.(you can create multiple app password)
并生成您的应用程序密码并保存以备将来使用。因为一旦您生成应用密码,您将无法重新编辑密码或更改相同的应用密码。(您可以创建多个应用密码)
回答by Faris Rayhan
This is working sample that i have tried :
这是我尝试过的工作示例:
Open your mail.php
under config
folder then fill with this option :
打开你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',
Open your .env
file under root
project. Also edit this file following above
option such
.env
在root
项目下打开您的文件。还可以按照上述选项编辑此文件,例如
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremailusername
MAIL_PASSWORD=youremailpassword
MAIL_ENCRYPTION=tls
After that clear your config by running this command
之后通过运行此命令清除您的配置
php artisan config:cache
Restart your local server
重新启动本地服务器
Try visit your route with controller contains mail function at first
time it still error Authentication Required
. You need to login via
your gmail account to authorize untrusted connection. Visit this linkto authorize
尝试使用控制器访问您的路由,第一次包含邮件功能,但仍然出错Authentication Required
。您需要通过您的 Gmail 帐户登录以授权不受信任的连接。访问此链接以授权
回答by Gokigooooks
if you still could be able to send mail after setting all configs right and get forbidden or timeout errors you could set the allow less secure apps to access your account
in gmail. you can follow how to here
如果您在正确设置所有配置后仍然可以发送邮件并收到禁止或超时错误,您可以allow less secure apps to access your account
在 gmail 中设置。你可以按照这里的方法
回答by Tumelo Mapheto
Note: Laravel 7 replaced MAIL_DRIVER by MAIL_MAILER
注意: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
Allow less secure apps from "Google Account" - https://myaccount.google.com/- Settings - Less secure app access (Turn On)
允许来自“Google 帐户”的不太安全的应用程序 - https://myaccount.google.com/- 设置 - 不太安全的应用程序访问(打开)
Flush cache config:
刷新缓存配置:
php artisan config:cache
For Apache:
对于阿帕奇:
sudo service apache2 restart
回答by Hisham Shami
you need to enable first the App password of your google account -> security section link
您需要先启用您的谷歌帐户的应用密码 -> 安全部分链接
then the app password that will be generated copy it and paste it in to .env file
然后将生成的应用程序密码复制并粘贴到 .env 文件中
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=app_password
MAIL_ENCRYPTION=ssl
回答by svikramjeet
If you are using email password then you should replace it with app password.for setting APP password you need to enable the 2 step authentication before setting password which can be disabled later.
如果您使用的是电子邮件密码,则应将其替换为应用程序密码。对于设置应用程序密码,您需要在设置密码之前启用两步验证,以后可以禁用。
Also make sure that you have allowed less secure app in setting section.For additional info you can follow how to here
还要确保您在设置部分允许不太安全的应用程序。有关其他信息,您可以按照此处操作
回答by jsepar00
Working for me after trying various combinations.
在尝试了各种组合后为我工作。
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=passowrd
MAIL_ENCRYPTION=ssl
It is necessary to generate application password https://myaccount.google.com/securityand us it as MAIL_PASSWORD
environment variable.
有必要生成应用程序密码https://myaccount.google.com/security并将其用作MAIL_PASSWORD
环境变量。
I found about about this by checking error code from google server, which was use-full and lead me to thiswebpage.
我通过检查来自谷歌服务器的错误代码发现了这一点,这是有用的,并引导我到这个网页。
回答by gtamborero
You can also try adding to laravel these 2 lines:
您也可以尝试将以下 2 行添加到 Laravel:
[email protected]
MAIL_FROM_NAME="[email protected]"
After this run this comand to flush mail cache config:
在此之后运行此命令以刷新邮件缓存配置:
php artisan config:cache