如何在Laravel 5.1中使用gmail发送邮件?
我一次又一次地尝试从本地主机发送电子邮件,但我仍然不能。 我不知道该怎么做。 我尝试searchfind解决scheme,但我找不到一个解决scheme。 我编辑了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
它仍然产生这样的错误:
首先login到您的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
在.env
文件中进行更改后,请不要忘记运行php artisan config:cache
。
尝试使用sendmail而不是smtp驱动程序(根据这些build议: 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
这是我尝试过的工作示例:
在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<br> MAIL_PASSWORD=youremailpassword MAIL_ENCRYPTION=tls
之后通过运行这个命令清除你的configuration
php artisan config:cache
重新启动您的本地服务器
尝试访问您的路由与控制器第一次包含邮件function它仍然错误Authentication Required
。 您需要通过您的Gmail帐户login来授权不可信的连接。 访问此链接进行授权
如果在设置好所有configuration并获取禁止或超时错误后,您仍然可以发送邮件,则可以设置allow less secure apps to access your account
在gmail中allow less secure apps to access your account
。 你可以按照如何在这里
所有你需要做的只是在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://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjMxo-KobTXAhUU148KHRCnA2cQFgglMAA&url=https%3A%2F%2Fsupport.google。; COM%2Faccounts%2Fanswer%2F185833%3Fhl%3Den&USG = AOvVaw2qwXmKRTjsa0k-q38HqJIX
并生成您的应用程序密码并保存以供将来使用。 因为一旦您生成应用程序密码,您不能重新编辑密码或更改相同的应用程序密码(您可以创build多个应用程序密码)
你的MAIL_PASSWORD=must a APPpasword
在更改MAIL_PASSWORD=must a APPpasword
后停止服务器然后清除configuration文件cahce php artisan config:cahce并重新启动服务器
引用无法在laravel 5.2中发送没有发件人地址的邮件我已经设置了.env和mail.php
如果您正在开发一个XAMPP,那么您将需要一个SMTP服务来发送电子邮件。 尝试使用MailGun帐户。 它免费且易于使用。