63

I'm trying to set up email for my first laravel project, and was thrilled that there's a laracast for it: https://laracasts.com/lessons/mailers

I've followed the simple steps, chose gmail in mail.php (x's added for anonymity):

'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 has (x's added for anonymity)- the laracast doesn't have us use MAIL_USERNAME and MAIL_PASSWORD, but they're updated for good measure assuming I'll pull from there later:

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

But in the first little moment of truth trying to send a mail (3:30 mark), I get an authentication error: Swift_TransportException in AuthHandler.php line 181: Failed to authenticate on SMTP server with username "dianexxxxx@gmail.com" using 3 possible authenticators

Based on a little stackoverflow reading, I tried turning on 'access to less secure apps' in my gmail settings, but no change in the error. (And the other posts about this issue had other factors that made a fancier problem than mine).

Nothing jumps out at me from laravel.log (below)- any ideas?

[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}  
Diane Kaplan
  • 1,626
  • 3
  • 24
  • 34

10 Answers10

169

This will not work as of May 30, 2022 checkout here https://support.google.com/accounts/answer/6010255?hl=en&authuser=6 Thanks @sarout for pointing this out.

Did you turn on the "Allow less secure apps" on? go to this link

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

Take a look at the Sign-in & security -> Apps with account access menu.

You must turn the option "Allow less secure apps" ON.

If is still doesn't work try one of these:

And change your .env file

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

because the one's you have specified in the mail.php will only be used if the value is not available in the .env file.

Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
  • 3
    thanks, Milan! Yup I'd tried that first suggestion but the other two are new. I just did them both (and did artisan cache:clear and artisan config:cache), but sadly there's still no change in the error I get :( – Diane Kaplan Nov 26 '15 at 13:31
  • did you get email telling that your sign-in attempt was prevented from gmail? – Milan Maharjan Nov 26 '15 at 14:10
  • change your .env file to the one I have mentioned above. – Milan Maharjan Nov 26 '15 at 14:15
  • 1
    thank you! I've updated this now (including the host and port), and the good news is I've moved on to a new error now (progress!): Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/answer/14257 h19sm6150583qgd.37 - gsmtp " – Diane Kaplan Nov 26 '15 at 15:00
  • 6
    I got this same error, the problem was access for less secure apps was not turned on, even though I turned it on. Try this new link https://www.google.com/settings/security/lesssecureapps Also I turned it on and when i refreshed the page, it was back to turned off. I again turned it on and refreshed and checked. Repeat this until the turn on is selected by default. I know this is crazy, but this worked for me :D – Milan Maharjan Nov 26 '15 at 15:09
  • 1
    Thanks @MilanMaharjan. Worked for me too! – Ihor Burlachenko Sep 04 '17 at 15:51
  • Didn't need the "less secure apps", but did need the app password & UnlockCaptcha. – thomas_inckx Jul 10 '19 at 15:51
  • Thanks @MilanMaharjan, using quotes for password worked for me! – Ketan Jan 02 '20 at 16:17
  • This worked for me, I had to after that delete cache – Diego Santa Cruz Mendezú Nov 10 '21 at 19:16
  • 1
    NOTE: allowing connected apps will be auto disabled in may 30 for security reasons, [check out more](https://support.google.com/accounts/answer/6010255?hl=ar&authuser=6) – Sarout Mar 16 '22 at 10:52
49

Nothing wrong with your method, it's a G-mail security issue.

  1. Login g-mail account settings.

  2. Enable 2-step verification.

  3. Generate app-password.

  4. Use new-generated password in place of your real g-mail password.

Don't forget to clear cache.

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
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
benjolly1989
  • 2,732
  • 1
  • 12
  • 3
  • Google has now removed this feature and recommends to login via app passwords after enabling 2 step verification. – Hadem Jun 30 '22 at 05:18
19

If you still get this error when sending email: "Failed to authenticate on SMTP server with username "youremail@gmail.com" using 3 possible authenticators"

You may try one of these methods:

  1. Go to https://accounts.google.com/UnlockCaptcha, click continue and unlock your account for access through other media/sites.

  2. Using a double quote password: "your password" <-- this one also solved my problem.

H45H
  • 1,019
  • 10
  • 28
  • 2
    Thanks! Works for me in `Laravel 7`. My `.env` file: `MAIL_PASSWORD="mypassword"` – alditis May 31 '20 at 08:19
  • This works for me... When opening a UnlockCaptcha link **"make sure your default google account _(out of many like in my case)_ is that you're using SMTP"** for _(I opened the smtp account in incognito)_ ... Otherwise it will confirm this security check for your default account and your SMTP account will remain blocked. – Daniyal Nasir Jan 15 '22 at 13:40
8

I had the same issue, but when I ran the following command, it was ok:

php artisan config:cache
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
3

Change the .env file as follow

MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=email_address@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

And the go to the gmail security section ->Allow Less secure app access

Then run

php artisan config:clear

Refresh the site

  • Is there an analogous method for Office365? The Gmail solution is easy and elegant - but all the Office365 solutions for authenticating seem to require admin privileges or external email providers. – Dr Dave Sep 18 '20 at 15:18
2

If you still get this error when sending email: "Failed to authenticate on SMTP server with username "example@gmail.com" using 3 possible authenticators"

You may try one of these methods:

Go to https://accounts.google.com/UnlockCaptcha, click continue and unlock your account for access through other media/sites.

Use double quote for your password: like - "Abc@%$67eSDu"

Shilwant Gupta
  • 149
  • 1
  • 4
1

This is how I solved this issue:

  1. Change the .env file as follow

Screenshot Reference

  1. Never forget to restart the server after you change the .env file
Raju
  • 2,902
  • 8
  • 34
  • 57
1

I had the same problem and I've already tried everything and nothing seemed to work until I just changed the 'host' value in config.php to:

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

When I changed that it worked nicely, somehow it was using the default host " smtp.mailtrap.org" and ignoring the .env variable I was setting.

After making some test I realize that if I placed the env variable in this order it would worked as it shoulded:

MAIL_HOST=smtp.mailtrap.io


MAIL_DRIVER=smtp


MAIL_PORT=2525


MAIL_USERNAME=xxxx


MAIL_PASSWORD=xxx


MAIL_ENCRYPTION=null

Luis Mata B.
  • 312
  • 3
  • 8
1

I also got the same error-

  1. Make sure less secure app access is enabled in your gmail account https://myaccount.google.com/security#connectedapps
  2. Put double quotes to the password "password" in .env file

These solutions worked for me.

Raj
  • 37
  • 8
1

I know this is too late to answer, but I solved this issue by adding double quotes to the password like so:

MAIL_PASSWORD="yourpasswordgoeshere"
Blues Clues
  • 1,694
  • 3
  • 31
  • 72