10

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

5
  • please run php artisan config:cache to recache .env Commented Oct 5, 2018 at 22:20
  • Ensure you've allowed less secure apps on your account: support.google.com/accounts/answer/6010255?hl=en Commented Oct 5, 2018 at 22:46
  • @KirkBeard i allowed this sir. Commented Oct 6, 2018 at 6:02
  • @kuromoka i already tried this sir.. result negative Commented Oct 6, 2018 at 6:02
  • 1
    Switch to using Mailgun and your headaches will be gone! Commented Nov 17, 2018 at 0:42

6 Answers 6

8

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

Sign up to request clarification or add additional context in comments.

1 Comment

the only answer
7

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

1 Comment

Thank you for help. But please remove last image as it may contain confidential information.
4

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

Comments

2

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"

2 Comments

sir i tried your following links and solution ... when i tried your top solution its showing error ... Error is: "Expected response code 250 but got code "530", with message "530-5.5.1 Authentication Required. Learn more at 530 5.5.1 support.google.com/mail/?p=WantAuthError q21-v6sm36318723pfq.51 - gsmtp " when i tried to login at Gmail suite its showing me " admin.google.com is for G Suite accounts only. Regular Gmail accounts cannot be used to sign in to admin.google.com"
So if we don't have GSuite, we're just hosed, huh? Not a great solution. My localhost emails worked until I switched to Laravel...
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.