K Arun Singh
K Arun Singh

Reputation: 810

Laravel 5 email error handeling when username or password is wrong

I'm working first time on sending mail using smtp in Laravel.

I have configured my gmail details and every thing is working fine and I'm getting mail to my inbox.

But I want to know how to handle error/exception when provided gmail information like Username or Password is wrong.

Right now when I provide a wrong password it is taking me to error screen with following message:

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/answer/14257 f191sm3044706pfa.26 - gsmtp"

I have tried adding some code in app\Exceptions\handler.php as below

public function render($request, Exception $e)
{
    //This is my code
    if ($e instanceof Swift_RfcComplianceException){
        return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
    }
    return parent::render($request, $e);
}

My smtp details in .env file are: (Working fine)

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_NAME=Arun Singh
[email protected]
MAIL_PASSWORD=mypassword

Code snippet for sending mail: (Working fine)

Mail::queue('/emails/test', array('firstname'=>'arun'), function($message){
    $to_email = '[email protected]';
    $to_firstname = 'Sanju';
    $to_lastname = 'Singh';
    $message->to($to_email, $to_firstname.' '.$to_lastname)->subject('Welcome to the XYZ!');
})

Thank you for your help.

Upvotes: 1

Views: 1492

Answers (2)

Y. Joy Ch. Singha
Y. Joy Ch. Singha

Reputation: 3252

In my .env file

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

Same error i got it but after long time i fixed it by

https://myaccount.google.com/lesssecureapps

Here, Less secure app access must be ON

Allow less secure apps: ON

Thanks.

Upvotes: 0

Ryukish
Ryukish

Reputation: 100

In your error handler, the exception that your trying to catch is

Swift_RfcComplianceException

But the exception that is being thrown is

Swift_TransportException

To catch the transport exception, just add this to your error handler:

if ($e instanceof \Swift_TransportException){
    return redirect($request->fullUrl())->with('error',"We have issue sending you an email");
}

Upvotes: 2

Related Questions