jeudi 26 mai 2016

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 http://ift.tt/1b3rDlG 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
MAIL_ADDRESS=myemail@gmail.com
MAIL_NAME=Arun Singh
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=mypassword

Code snippet for sending mail: (Working fine)

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

Thank you for your help.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Wl8CQR
via IFTTT

Aucun commentaire:

Enregistrer un commentaire