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
MAIL_USERNAME=myemailID@gmail.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls

Controller Code Below :

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

    Mail::send('email', $data, function($message) {
        $message->to('ToEmailID@yahoo.com','To My Yahoo')->subject('Laravel Test Email');
        $message->from('ToFromID@gmail.com','Ripon Uddin (Laravel Lover)');
    });

Error gettings :

Failed to authenticate on SMTP server with username "myEmailID@gmail.com" 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

Ripon Uddin
  • 709
  • 3
  • 14
  • 29

6 Answers6

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

Haykins
  • 101
  • 1
  • 3
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

Nur Uddin
  • 1,798
  • 1
  • 28
  • 38
4

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

Qaisar Shabbir
  • 103
  • 2
  • 10
  • 1
    Thank you for help. But please remove last image as it may contain confidential information. – Jsowa Dec 16 '22 at 23:14
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: xxxx@xxxxxxx.com
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: xxx@xxxxxxx.com
mailer_password: "xxxx"
Tsounabe
  • 2,109
  • 1
  • 16
  • 25
  • 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 https://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" – Ripon Uddin Oct 06 '18 at 06:18
  • 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... – McAuley Feb 07 '19 at 00:52
2

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Red Devil
  • 111
  • 5
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.

Bhargav Variya
  • 725
  • 1
  • 10
  • 18