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

时间:2015-09-11 04:03:17

标签: php email gmail laravel-5.1

我一次又一次地尝试测试从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' => '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

它仍然产生如下错误: enter image description here

18 个答案:

答案 0 :(得分:115)

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

您的.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

答案 1 :(得分: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
MAIL_USERNAME=your@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

答案 2 :(得分: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

为app密码转到 https://support.google.com/accounts/answer/185833?hl=en

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

答案 3 :(得分:12)

这是我尝试的工作样本:

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

.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 授权

答案 4 :(得分:7)

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

的方式进行操作

答案 5 :(得分: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

答案 6 :(得分:1)

您需要先启用Google帐户的应用密码->安全部分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

答案 7 :(得分:0)

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

from termcolor import colored
print(colored('Hello'.center(100), 'green', attrs=['bold']))

答案 8 :(得分: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 password,如果您看不到该选项,则可能未启用2因子身份验证。如果您采用这种方法,则无需允许安全性较低的应用程序。

答案 9 :(得分: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安全设置中的帐户。

答案 10 :(得分:0)

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

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

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

php artisan config:cache

答案 11 :(得分: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服务器的错误代码(已用完)并引导我进入this网页来找到有关此问题的信息。

答案 12 :(得分:0)

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

还请确保您在“设置”部分中允许使用的安全性较差的应用。有关其他信息,请按照here

答案 13 :(得分:0)

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

MAIL_DRIVER=sendmail

答案 14 :(得分:0)

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

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

如果您的用户名/密码已被删除,只需设置:

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

答案 15 :(得分:0)

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

答案 16 :(得分:0)

你的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

答案 17 :(得分:-1)

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

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