Wednesday, 16 January 2019

Send Emails Using Gmail SMTP in Laravel

Creating and configuring your Laravel application to send emails using Gmail's SMTP server is a straightforward process that can significantly enhance the functionality of your web application.
 

Step: 1 Obtain Gmail SMTP Credentials

Firstly, we need to have a Gmail account. If you're using this for a production environment, it might be wise to create a dedicated account. Once you have your account, you need to generate an app-specific password. This is a more secure way of using Gmail SMTP, as it doesn't involve using your actual Gmail password. To generate an app-specific password, go to your Google Account settings, navigate to the "Security" tab, and under "Signing in to Google," choose "App passwords." You may need to sign in again. Select the app and device you want to generate the password for and follow the prompts.

Add the below code in the .env file

We need to update our Gmail username and password.


Note: For enhanced security, it's recommended to generate an App Password if you have 2-Step Verification enabled on your Google account.


Step 2: Configure Laravel Environment


Step: 3 Update config/mail.php


Ensure your config/mail.php reflects the .env settings by using the env function to pull in these values. Laravel's default configuration should automatically align with your .env file, but it's good practice to double-check.

Step: 4 Sending email in Laravel controller


Controller

Simply update $to_email your email where you want to have an email.
The above code will send an email using gmail smtp settings.

No comments:

Post a Comment

Laravel csrf token mismatch for ajax post request

Error "CSRF Token Mismatch" commonly occurs if tokens do not match in both sessions and sent , and received requests.CSRF token he...