SMTP Gmail Authentication Fix

User
SymfonyComponentMailerExceptionTransportException: Failed to authenticate on SMTP server with username "neeraj.tiw1988@gmail.com" using the following authenticators: "LOGIN", "PLAIN", "XOAUTH2". Authenticator "LOGIN" returned "Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials d2e1a72fcca58-737115511cfsm12518716b3a.45 – gsmtp".". Authenticator "PLAIN" returned "Expected response code "235" but got code "535", with message "535-5.7.8 Username and Password not accepted. For more information, go to
535 5.7.8 https://support.google.com/mail/?p=BadCredentials d2e1a72fcca58-737115511cfsm12518716b3a.45 – gsmtp".". Authenticator "XOAUTH2" returned "Expected response code "235" but got code "334", with message "334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==".". in /var/www/html/vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php:242
Stack trace:
#0 /var/www/html/vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php(177): SymfonyComponentMailerTransportSmtpEsmtpTransport->handleAuth()
#1 /var/www/html/vendor/symfony/mailer/Transport/Smtp/EsmtpTransport.php(134): SymfonyComponentMailerTransportSmtpEsmtpTransport->doEhloCommand()
#2 /var/www/html/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(249): SymfonyComponentMailerTransportSmtpEsmtpTransport->executeCommand()
#3 /var/www/html/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(272): SymfonyComponentMailerTransportSmtpSmtpTransport->doHeloCommand()
#4 /var/www/html/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(209): SymfonyComponentMailerTransportSmtpSmtpTransport->start()
#5 /var/www/html/vendor/symfony/mailer/Transport/AbstractTransport.php(69): SymfonyComponentMailerTransportSmtpSmtpTransport->doSend()
#6 /var/www/html/vendor/symfony/mailer/Transport/Smtp/SmtpTransport.php(137): SymfonyComponentMailerTransportAbstractTransport->send()
#7 /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(585): SymfonyComponentMailerTransportSmtpSmtpTransport->send()
#8 /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/Mailer.php(332): IlluminateMailMailer->sendSymfonyMessage()
#9 /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(205): IlluminateMailMailer->send()
#10 /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php(19): IlluminateMailMailable->IlluminateMail{closure}()
#11 /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php(198): IlluminateMailMailable->withLocale()
#12 /var/www/html/vendor/laravel/framework/src/Illuminate/Mail/SendQueuedMailable.php(83): IlluminateMailMailable->send()
#13 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): IlluminateMailSendQueuedMailable->handle()
#14 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#15 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): IlluminateContainerUtil::unwrapIfClosure()
#16 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): IlluminateContainerBoundMethod::callBoundMethod()
#17 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): IlluminateContainerBoundMethod::call()
#18 /var/www/html/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(128): IlluminateContainerContainer->call()
#19 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): IlluminateBusDispatcher->IlluminateBus{closure}()
#20 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): IlluminatePipelinePipeline->IlluminatePipeline{closure}()
#21 /var/www/html/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php(132): IlluminatePipelinePipeline->then()
#22 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(124): IlluminateBusDispatcher->dispatchNow()
#23 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): IlluminateQueueCallQueuedHandler->IlluminateQueue{closure}()
#24 /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): IlluminatePipelinePipeline->IlluminatePipeline{closure}()
#25 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(123): IlluminatePipelinePipeline->then()
#26 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/CallQueuedHandler.php(71): IlluminateQueueCallQueuedHandler->dispatchThroughMiddleware()
#27 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Jobs/Job.php(102): IlluminateQueueCallQueuedHandler->call()
#28 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(440): IlluminateQueueJobsJob->fire()
#29 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(390): IlluminateQueueWorker->process()
#30 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Worker.php(177): IlluminateQueueWorker->runJob()
#31 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(139): IlluminateQueueWorker->daemon()
#32 /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Console/WorkCommand.php(122): IlluminateQueueConsoleWorkCommand->runWorker()
#33 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): IlluminateQueueConsoleWorkCommand->handle()
#34 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Util.php(43): IlluminateContainerBoundMethod::IlluminateContainer{closure}()
#35 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(95): IlluminateContainerUtil::unwrapIfClosure()
#36 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): IlluminateContainerBoundMethod::callBoundMethod()
#37 /var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php(690): IlluminateContainerBoundMethod::call()
#38 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(213): IlluminateContainerContainer->call()
#39 /var/www/html/vendor/symfony/console/Command/Command.php(279): IlluminateConsoleCommand->execute()
#40 /var/www/html/vendor/laravel/framework/src/Illuminate/Console/Command.php(182): SymfonyComponentConsoleCommandCommand->run()
#41 /var/www/html/vendor/symfony/console/Application.php(1047): IlluminateConsoleCommand->run()
#42 /var/www/html/vendor/symfony/console/Application.php(316): SymfonyComponentConsoleApplication->doRunCommand()
#43 /var/www/html/vendor/symfony/console/Application.php(167): SymfonyComponentConsoleApplication->doRun()
#44 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(197): SymfonyComponentConsoleApplication->run()
#45 /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1203): IlluminateFoundationConsoleKernel->handle()
#46 /var/www/html/artisan(13): IlluminateFoundationApplication->handleCommand()
#47 {main}
Assistant
The error message indicates that your Laravel application is unable to authenticate with Gmail's SMTP server due to incorrect credentials or security restrictions.

### Steps to Fix the Issue:

#### 1. **Check Email and Password**
Make sure that your `.env` file contains the correct email and password for Gmail.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your-email@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email@gmail.com
MAIL_FROM_NAME="Your Name"

If you are using Gmail, **do not** use your regular Gmail password. You must generate an **App Password**.

#### 2. **Enable App Password in Google Account**
Google restricts third-party access to Gmail accounts. You need to generate an App Password:

1. Go to your [Google Account Security Page](https://myaccount.google.com/security).
2. Under **"Signing in to Google"**, enable **2-Step Verification** (if not already enabled).
3. Once 2-Step Verification is enabled, go to **"App Passwords"**.
4. Select **Mail** as the app and **Other (Custom name)** for the device.
5. Generate the password and copy it.
6. Use this password in `MAIL_PASSWORD` in your `.env` file.

#### 3. **Clear Cache**
After updating the `.env` file, clear the cache:

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

#### 4. **Check Less Secure App Access (if needed)**
Google has deprecated the "Less Secure Apps" option, but if you are using an older Google account setup, you may need to check [this Google support page](https://support.google.com/accounts/answer/6010255).

#### 5. **Use OAuth2 (Alternative)**
If you don't want to use an App Password, you can use OAuth2 authentication. However, this is more complex and requires setting up Google OAuth credentials.

#### 6. **Test SMTP Connection**
Try testing the SMTP configuration manually with the following command:

php artisan tinker

Then run:

use IlluminateSupportFacadesMail;

Mail::raw('Test email', function ($message) {
    $message->to('your-email@gmail.com')->subject('Testing SMTP');
});

If the email sends successfully, then your SMTP configuration is working.

Let me know if you need more assistance! ๐Ÿš€

Leave a Comment

Your email address will not be published. Required fields are marked *