使用gmail时出现SMTP服务器身份验证失败错误

63

我正在尝试为我的第一个Laravel项目设置电子邮件,很高兴发现有一个针对此的Laracast:https://laracasts.com/lessons/mailers

我按照简单的步骤进行操作,在mail.php中选择了Gmail(由于隐私原因我用x代替了一些信息):

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'dianexxxxx@gmail.com', 'name' => 'Diane Kaplan'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => 'dianexxxxx@gmail.com',
'password' => 'xxxxx',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,

.env文件中有一些添加的x以保护隐私 - laracast没有让我们使用MAIL_USERNAME和MAIL_PASSWORD,但出于良好的考虑我会稍后从那里拉取最新更新:

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=dianexxxxx@gmail.com
MAIL_PASSWORD=xxxxxx
MAIL_ENCRYPTION=null

但是,在尝试发送电子邮件的第一个真正的时刻(3:30),我遇到了身份验证错误:

Swift_TransportException in AuthHandler.php line 181: 使用3个可能的身份验证器中的用户名“dianexxxxx@gmail.com”无法在SMTP服务器上进行身份验证

根据一些stackoverflow阅读,我尝试在我的gmail设置中启用“访问不安全的应用程序”,但错误没有改变。(而且其他关于此问题的帖子有其他因素,使问题比我的更复杂。)

laravel.log文件中没有跳出任何问题-有什么想法吗?

[2015-11-26 12:25:55] local.ERROR: exception 'Swift_TransportException' with message 'Failed to authenticate on SMTP server with username "dianekaplan@gmail.com" using 3 possible authenticators' in /home/vagrant/Code/Family-laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/Esmtp/AuthHandler.php:181
Stack trace:
    #0 /home/vagrant/Code/Family-laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php(307): Swift_Transport_Esmtp_AuthHandler->afterEhlo(Object(Swift_SmtpTransport))
    #1 /home/vagrant/Code/Family-laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(118): Swift_Transport_EsmtpTransport->_doHeloCommand()
    #2 /home/vagrant/Code/Family-laravel/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start()
    #3 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(395): Swift_Mailer->send(Object(Swift_Message), Array)
    #4 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(181): Illuminate\Mail\Mailer->sendSwiftMessage(Object(Swift_Message))
    #5 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(220): Illuminate\Mail\Mailer->send('emails.welcome', Array, Object(Closure))
    #6 /home/vagrant/Code/Family-laravel/app/Http/routes.php(59): Illuminate\Support\Facades\Facade::__callStatic('send', Array)
    #7 /home/vagrant/Code/Family-laravel/app/Http/routes.php(59): Illuminate\Support\Facades\Mail::send('emails.welcome', Array, Object(Closure))
    #8 [internal function]: App\Providers\RouteServiceProvider->{closure}()
    #9 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(155): call_user_func_array(Object(Closure), Array)
    #10 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Route.php(130): Illuminate\Routing\Route->runCallable(Object(Illuminate\Http\Request))
    #11 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(704): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
    #12 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
    #13 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #15 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #16 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(706): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #17 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(671): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
    #18 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Routing/Router.php(631): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
    #19 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
    #20 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
    #21 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #22 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #23 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
    #24 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #25 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #26 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
    #27 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #28 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #29 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
    #30 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #31 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #32 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
    #33 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #34 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #35 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
    #36 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #37 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #38 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
    #39 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array(Array, Array)
    #40 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #41 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
    #42 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #43 /home/vagrant/Code/Family-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
    #44 /home/vagrant/Code/Family-laravel/public/index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
    #45 {main}  
11个回答

170

从2022年5月30日起,此方法将无效,请点此查看https://support.google.com/accounts/answer/6010255?hl=en&authuser=6。感谢@sarout指出这一点。

您是否开启了“允许不安全的应用程序”?请前往此链接:

https://myaccount.google.com/security#connectedapps

查看“登录与安全” -> “具有帐户访问权限的应用程序”菜单。

您必须打开选项“允许不安全的应用程序”。

如果仍然无法使用,请尝试以下方法之一:

并更改您的.env文件。

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=dianexxxxx@gmail.com
MAIL_PASSWORD=xxxxxx
MAIL_ENCRYPTION=tls

因为在mail.php中指定的值只有在.env文件中没有相应值可用时才会被使用。


3
谢谢,米兰!是的,我先尝试了那个建议,但其他两个是新的。我刚刚都尝试了(并且执行了“artisan cache:clear”和“artisan config:cache”命令),但遗憾的是我得到的错误仍然没有改变。 - Diane Kaplan
1
谢谢!我现在已经更新了(包括主机和端口),好消息是我遇到了一个新的错误(进展了!):期望响应代码为250,但得到的代码是“535”,并显示以下信息:“535-5.7.8 用户名和密码不被接受。请访问https://support.google.com/mail/answer/14257 h19sm6150583qgd.37 - gsmtp 了解更多信息。” - Diane Kaplan
6
我遇到了同样的错误,问题是未开启不安全应用程序的访问权限,尽管我已经打开了它。请尝试使用此新链接:https://www.google.com/settings/security/lesssecureapps同时我也打开了它,但当我刷新页面时,它又变成了关闭状态。我又重新打开并刷新并检查。重复此操作,直到默认选中打开为止。我知道这很疯狂,但这对我有用 :D - Milan Maharjan
1
谢谢@MilanMaharjan。对我也有用! - Ihor Burlachenko
1
注意:出于安全原因,允许连接的应用程序将在5月30日自动禁用,了解更多 - Sarout
显示剩余5条评论

50

你的方法没有问题,这是G-mail的安全问题。

  1. 登录g-mail账户设置。

  2. 启用两步验证。

  3. 生成应用程序密码

  4. 使用新生成的密码替换真实的g-mail密码。

别忘了清除缓存。

php artisan config:cache.
php artisan config:clear.

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=587
MAIL_USERNAME=yourgmailid@gmail.com
MAIL_PASSWORD=generatedAppPassword
MAIL_ENCRYPTION=tls

谷歌现已删除此功能,并建议在启用两步验证后通过应用程序密码登录。 - Hadem
这对我来说是正确的答案。+1 - undefined

19
如果您在发送电子邮件时仍然遇到以下错误:“尝试使用3种可能的身份验证器使用用户名为“youremail@gmail.com”的SMTP服务器进行身份验证失败”,您可以尝试以下方法之一:
1. 前往https://accounts.google.com/UnlockCaptcha,单击“继续”并解锁您的帐户以通过其他媒体/站点访问。 2. 使用双引号密码:“your password” <--这个方法也解决了我的问题。

2
谢谢!在 Laravel 7 中对我有用。我的 .env 文件:MAIL_PASSWORD="mypassword" - alditis
这对我有用...当打开一个UnlockCaptcha链接时,**"确保你的默认谷歌账户(在我的情况下有多个)是你正在使用SMTP的那个"**...否则它将确认你的默认账户的安全检查,而你的SMTP账户将保持被阻止状态。(我在无痕模式下打开了SMTP账户)。 - Daniyal Nasir

8

我也遇到了同样的问题,但是当我运行了以下命令后,问题解决了:

php artisan config:cache

2
如果您在发送电子邮件时仍然收到此错误:“使用3个可能的验证器在SMTP服务器上无法使用用户名“example@gmail.com”进行身份验证”,您可以尝试以下方法之一:
1. 前往https://accounts.google.com/UnlockCaptcha,点击继续并解锁您的帐户以通过其他媒体/站点访问。 2. 使用双引号包含您的密码:例如 - "Abc@%$67eSDu"。

2
将 .env 文件更改如下:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=email_address@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

前往 Gmail 安全设置页面

->启用“较不安全的应用访问”

然后运行

php artisan config:clear

刷新网站


有没有类似的方法适用于Office365? Gmail解决方案简单而优雅 - 但是所有Office365身份验证的解决方案似乎都需要管理员权限或外部电子邮件提供商。 - Dr Dave

1
这是我解决这个问题的方法:
  1. 按照以下方式更改.env文件

Screenshot Reference

  1. 在更改 .env 文件后,永远不要忘记重新启动服务器。

任何环境属性的更改都需要重新启动服务器。 - Ankit Tanna

1

1

我知道回答有些晚了,但是我通过给密码加上双引号来解决了这个问题,就像这样:

MAIL_PASSWORD="yourpasswordgoeshere"

1

我曾遇到同样的问题,尝试了各种方法都无效,直到我将config.php中的“host”值更改为以下内容:

 'host' => env('smtp.mailtrap.io'),

当我更改后,它很好地工作了,不知何故它使用默认主机“smtp.mailtrap.org”,并忽略我设置的.env变量。

经过一些测试,我意识到如果按照这个顺序放置env变量,它将按照应该的方式工作:

MAIL_HOST=smtp.mailtrap.io

MAIL_DRIVER=smtp

MAIL_PORT=2525

MAIL_USERNAME=xxxx

MAIL_PASSWORD=xxx

MAIL_ENCRYPTION=null


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接