In my previous blog, I described how can you send an email with an attachment without saving it in the database you can read that here.
Now let’s continue with this topic. If you are laravel lover then definitely somehow it comes in your mind that how I can send an email through my own Gmail account … Here is the solution for it
In this tutorial, we will discuss how to configure our Laravel applications to send emails using your Gmail account as a Gmail SMTP server with the default Laravel SMTP configurations.
Step 1: Configure Gmail SMTP Server in Laravel Application
Laravel uses config/mail.php
file for storing details used in mail sending. This file contains settings like MAIL_DRIVER, MAIL_HOST, MAIL_PORT, etc. In order to successfully send an email, we need to provide this information.
To add this required information, we need not edit this config/mail.php
file, rather we would supply these details accordingly in the .env
file.
Thus, open your .env
file which is located in your root directory of our application and checkout for this settings:
Now edit the details above as follows.
- MAIL_DRIVER= smtp
- MAIL_HOST= smtp.googlemail.com
- MAIL_PORT= 465
- MAIL_USERNAME=your Gmail username
- MAIL_PASSWORD=your Gmail password
- MAIL_ENCRYPTION= ssl
Step 2: Configure your Google Account
Login to your Google Email Account and click on Google Account Button. This button is displayed when you click on the profile picture in your Gmail Dashboard as shown.
Once you are on My Account Page then click on Security and scroll down to the bottom and you will find ‘Less secure app access’ settings. Click on the radio button to set it ON.
Step 3: Send Emails from your Laravel Application
At this point, all the basic setup has been completed. We can now write some Laravel PHP codes to send an email.
To get started, create any controller of choice where the mail sending logic will be handled, then in this controller write your codes using the code snippet below as a guide.
In the above code, we are using our mail template as ’emails.mail’ file. hence we need to create an ‘emails’ folder and the mail.blade.php
file at resources\views\emails\mail.blade.php
Our test mail template mail.blade.php
should just contain a few test codes as shown below.
Hello <strong>{{ $name }}</strong>,
<p>{{body}}</p>
We are done, simply create any route of your choice to and start sending mails from your Laravel application. Enjoy
- Laravel – select dropdowns that filter results in a table ‘on change’ - October 30, 2021
- What is Rest and Restful API? Understand the Concept - September 5, 2020
- What is API, clear the Concept. - September 5, 2020