Header Ads

ad

Gmail setup in Laravel application with 2 major error handling



In this blog we will see how to configure the gmail with our Laravel application. Mail setup is a necessary thing to send important mails like reset password links. So what we need to have, a Gmail account and a Laravel application.

Assuming that you have a Gmail account, we are proceeding. 

If you already setup your basic Laravel project without proper mail configuration and have tried to reset the password of a registered user then you might have got errors like


  • stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines: SSL3_GET_SERVER_CERTIFICATE: certificate verify failed
  • Expected response code 250 but got code “535”, with message "535-5.7.8 Username and Password not accepted

In both the cases the configuration haven't properly done, so we need to configure our application with the mail in the actual and secured way. Let us start.


Step 1 : Enable 2 Step verification to the Google

For a secure mail setup we have to configure gmail in accordance with the security. We will enable 2 step verification from https://www.google.com/landing/2step/, where you have to press "Get Started" button and follow the steps to enable it. 


Step 2 : Create app password to be used as mail password

After enable 2-step verification, we must have to create app password so that our app can access our account accordingly. Now to setup gmail account with our application we need to have a password which will generate by the Google. We can create app password from https://security.google.com/settings/security/apppasswords, where we will be requested to login to our account (which we want to send mail from). We have to follow the steps as follows
  • Select the app (Custom name)
  • Provide the name (Laravel Demo)
  • Generate
Now you will have 16 character app password with you.
***Note: Please do not share the generated password with anyone.


Step 3 : Modify .env file 

Open .env file in any code editor and provide the mail setup details there. Modify the following lines

..
..

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=the 16 character generated app password
MAIL_ENCRYPTION=tls
..
..

Step 4 : Modify config > mail.php

In the config directory all files are essential as they all are the configurations of our app. Mail.php is the mail configuration file, where we have to modify as per our need. Modify the lines as following

..
..
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
..
..

'from' => [

     'address' => env('MAIL_FROM_ADDRESS', 'your_email@gmail.com'),

     'name' => env('MAIL_FROM_NAME', 'Your Name'),

],
..
..

That's it, we have a laravel application with the gmail setup.

***Note: If we got an error like "stream_socket_enable_crypto(): SSL operation failed with code 1..." this means our ssl certificate is not working. To handle this kind of error we have to follow the next step.

Step 5 : Error handling (stream_socket_enable_crypto(): SSL operation failed with code 1...)

Open mail.php in any code editor and add the codes at the bottom of the file like the following

'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ],


We are done with the gmail setup. Restart the server, now if we try to send reset password email, it will send us the mail with the link in the inbox.



Hope you liked it. Please share with everyone. Thanks

No comments