Ripon Uddin
Ripon Uddin

Reputation: 714

Laravel Failed to authenticate on SMTP server with username

Am trying to send email by Laravel. But It's getting error.

.env file Configuration below :

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

Controller Code Below :

 $data = array('name'=>"Ripon Uddin", "body" => "Test mail");

    Mail::send('email', $data, function($message) {
        $message->to('[email protected]','To My Yahoo')->subject('Laravel Test Email');
        $message->from('[email protected]','Ripon Uddin (Laravel Lover)');
    });

Error gettings :

Failed to authenticate on SMTP server with username "[email protected]" using 3 possible authenticators. Authenticator LOGIN returned Swift_TransportException: Expected response code 235 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/?p=BadCredentials r18-v6sm7406256pgv.17 - gsmtp " in

Upvotes: 10

Views: 58559

Answers (6)

Qaisar Shabbir
Qaisar Shabbir

Reputation: 139

Google changed its security policy. Now "Allow less secure apps" option is not available under Sign-in & security.

The idea is that Gmail does not allow any application to access a user's Gmail account, even with its real username and password. That's why people are getting the error. The new mechanism is you should create an application in your Google account and Google will generate a password for that application. Gmail will allow access with that Password and real username/Gmail ID.

The solution is

Step 1: sign-in to your Gmail account and to "Manage Your Account" as seen in the image below

enter image description here

Step 2: Click on the Security Tab in the left sidebar,

enter image description here

Step 3: Enable 2-step verification first for SMTP access.

enter image description here

You will not see "App Password" option until you enable 2-step verification. After enabling 2-step verification click on "App Password". You will see like this

If you still not seeing the "App Password" option, then search for it in the search bar like below.

enter image description here

Step 4: Now click on it.

enter image description here

Click on the first dropdown and select "Other (Custom Name)". In the new window write your application name and git the Generate button. Copy the password that showed up.

Use this password, instead of your real Gmail password. SMTP configuration should look like

enter image description here

Upvotes: 7

Bhargav Variya
Bhargav Variya

Reputation: 765

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.

Upvotes: 0

Red Devil
Red Devil

Reputation: 149

Make sure your user credentials are right in .env file.

Try putting your passwords in quotes. eg: MAIL_PASSWORD="mypassword"

To apply your changes Run

php artisan config:cache

Upvotes: 1

Nur Uddin
Nur Uddin

Reputation: 1840

Go to this link or app password page of gmail then select 'mail' as "app" and custome name as "device". Then you will get app password. Just go to your laravel mai.php under config folder in production or env file in dev mode. Paste the generated code on password section 'password' => 'generated code', After all run: php artisan config:cache

Upvotes: 4

Haykins
Haykins

Reputation: 111

you don't need to have GSuite to do this... I have done this integration alot of times with Laravel & GMail. Just go to your Google Account > Security > Less secure app access and Click on "Turn on access (not recommneded)"See image for more info

Upvotes: 8

Tsounabe
Tsounabe

Reputation: 2184

This config works on Gmail if you "Allow less secure apps" setting enabled in your gmail settings:

mailer_transport: smtp
mailer_host: smtp.gmail.com
mailer_port: 465
mailer_encryption: ssl
mailer_auth_mode: login
mailer_user: [email protected]
mailer_password: "xxxx"

This one works on Gmail Suite if you white list the ip of your server in your gmail settings (see https://support.google.com/a/answer/2956491):

mailer_transport: smtp
mailer_host: smtp-relay.gmail.com
mailer_port: 587
mailer_encryption: tls
mailer_auth_mode: login
mailer_user: [email protected]
mailer_password: "xxxx"

Upvotes: 2

Related Questions