Search code examples
emailsendmaillinux-mintlaravel-5.4

mail sending issue in laravel 5.4


I am trying to send an email using laravel 5.4. here is my .env

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

here is my HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Mail;
use App\Mail\MyTestMail;

class HomeController extends Controller
{

    /**
     * Send My Test Mail Example
     *
     * @return void
     */
    public function myTestMail()
    {
        //echo 'hello';
        $myEmail = 'example@gmail.com';
        Mail::to($myEmail)->send(new MyTestMail());


        dd("Mail Send Successfully");
    }

}

But i got an error like

Swift_TransportException in AbstractSmtpTransport.php line 383: 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/?p=BadCredentials r67sm38867592pfb.125 - gsmtp "

at Swift_Transport_AbstractSmtpTransport->_assertResponseCode('535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials r67sm38867592pfb.125 - gsmtp ', array(250)) in AbstractSmtpTransport.php line 281
at Swift_Transport_AbstractSmtpTransport->executeCommand('RSET ', array(250), array()) in EsmtpTransport.php line 270
at Swift_Transport_EsmtpTransport->executeCommand('RSET ', array(250)) in XOAuth2Authenticator.php line 55
at Swift_Transport_Esmtp_Auth_XOAuth2Authenticator->authenticate(object(Swift_SmtpTransport), 'username@gmail.com', 'password') in AuthHandler.php line 176
at Swift_Transport_Esmtp_AuthHandler->afterEhlo(object(Swift_SmtpTransport)) in EsmtpTransport.php line 332

Then i changed to my .env file to

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username2@gmail.com
MAIL_PASSWORD=password2
MAIL_ENCRYPTION=tls

then i restart apache2.But still the same error with previous username and password.i don't know why this happens?please help me.


Solution

  • Google now doesn't accept login from less secure apps

    You need to authorise the external application use of Gmail.

    Follow the next steps and authorise to make it:

    First, go to your Google Account Management page

    Under the Security options, click Edit link of Authorising applications & sites

    or

    You can allow less secure apps from your settings in Gmail, go to:

    https://www.google.com/settings/security/lesssecureapps

    and set 'Access for less secure apps' to "Enabled"

    Hope it helps you.