You can easily configure Gmail SMTP in Laravel 7 for sending an email. Laravel comes with different API based email drivers such as Mailgun, Sendgrid, etc. These API based email providers are much faster and secure for sending emails. You can use any of these for the faster service. These are much faster than SMTP. But, in this post, I will be dealing with how to send mail in Laravel 7 using your Gmail account. I will be using the Gmail SMTP for sending the emails. Later, I will come up with the API based email drivers and will post some tutorials. So, let’s dive into the post.
Prerequisites
Before going to create a new project in Laravel 7, you will require the following tools with a specified version.
- PHP >= 7.2.5
- MySQL > 5
- Apache/Nginx Server
- VS Code Editor (Optional)
- Composer
I prefer to use VS Code as an editor and I recommend you for the same.
Send Mail in Laravel 7
For sending email in Laravel 7, we will start with a new project. So, let’s create a new project first by using the composer command.
composer create-project --prefer-dist laravel/laravel send-email-using-gmail
You will have to wait while the above command will create and install the Laravel 7 project. Inside the project folder, it will install the necessary libraries and classes.
Laravel 7 Upload Multiple Images with Image Validation
Configure Gmail Account to Send Mail in Laravel 7
After login your Gmail account, go to the option Manage your Google Account by clicking on the profile. Now, inside the Security option, turn ON to allow the less secure app access.
Secondly, you will have to enable two-step verification method for your account. This will enhance the security of your account.
Lastly, you will need to generate an app password. Actually, you cannot use your real Gmail password for sending any emails in the Laravel email configuration. That’s why you will have to generate the app password. This password will be encrypted and it can be generated many times. Also, you can track the usage of this password for the application where you have used it. This is just for the security purposes of the Gmail account.
Now, come back to the Laravel application where you have created. Open it in the editor. Now, we will configure the SMTP for sending the emails.
How to Create a CRUD Application in Laravel 7
Configure Gmail SMTP to Send Mail in Laravel 7
For the SMTP configuration in Laravel 7, you will have to navigate to the .env file of the project. For SMTP configuration, you will have to put the username, password, port, hostname, and the encryption method.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=YOUR_GMAIL_USERNAME
MAIL_PASSWORD=YOUR_GENERATED_PASSWORD
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
Replace the username and password with yours. For the security purposes, Gmail won’t allow you to put the regular password as an open characters. That’s why we have generated the encrypted app password.
Once, you have configured the SMTP, your project is synchronized with the Gmail account. Now, you will have to create the Mail class to send mail in Laravel 7.
Create Mail Class to Send Mail in Laravel 7
You can create the Mail class by using the artisan command. Laravel provides the Mail helper class for sending emails. This can be created by hitting the below command.
php artisan make:mail MyEmail
Here, mail class has been created.
You can find the mail class inside the project folder app/Mail.
Now, write some code inside this Mail class.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class MyEmail extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->from('admin@programmingfields.com')->view('email.mail-template');
}
}
In the above code, I have returned a view that we’ll create in the next step. Here, we will have to set the sender of the Email. Also, you can pass the other parameters like the Sender Name, Reply Email etc.
How to Implement Google Charts in Laravel 7
Create a View For Email Template
In Laravel, you can find the view inside the resources folder. So, according to the above mail class snippet, we will create a folder inside the views folder with the name email. Then, inside that folder, we will create the view with the name mail-template.blade.php.
Now, inside this view paste the simple HTML snippet for a basic email template.
<!doctype html>
<html lang="en">
<head>
<title>Send Email in Laravel 7 Using Gmail SMTP | Programming Fields</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xl-6 col-lg-6 col-sm-12 m-auto">
<h3> Send Email in Laravel 7 Using Gmail </h3>
<p> Hey, </p>
<p> Welcome to Programming Fields </p>
<p> This is a basic demo for sending email in Laravel 7 using Gmail SMTP </p>
<br/>
<br/>
<p> Best Regards</p>
<p> Team, Programming Fields </p>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
How to Use Http Client For Request Handling in Laravel 7
Create Controller to Send Mail in Laravel 7
We will create a controller for implementing the functionality to send mail in Laravel. So, open the terminal or command prompt and enter the below command.
php artisan make:controller EmailController
The above command will create a controller with the name EmailController.php. You can find the controller inside the app/Http/Controllers.
Now, paste the below function to send mail in Laravel.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\MyEmail;
class EmailController extends Controller
{
// ------------- [ Send email ] --------------------
public function sendEmailToUser() {
$to_email = "umesh.rana0269@gmail.com";
Mail::to($to_email)->send(new MyEmail);
return "<p> Your E-mail has been sent successfully. </p>";
}
}
In the above function, I have passed the email recipient as static. You can make it dynamic by reading the email by a form through the inputs. In the next line, I have called the Mail class and passed the email recipient to send email in Laravel.
How to Implement Yajra DataTable in Laravel 7
Create Route
So for the above controller function, we will create a route inside the routes/web.php file.
Route::get("send-email", "EmailController@sendEmailToUser");
After creating the route, let’s run the project in the browser. You can access the URL by using the above route.
http://localhost:8000/send-email
As a result, you will see the email has been sent successfully to the recipient.
For the result, you can check the Gmail account here, I have received the email.
Laravel 7 RESTful APIs with Passport Authentication
Conclusion
Finally, we have completed the entire steps to send mail in Laravel 7 using Gmail SMTP. The steps consists of Gmail account configuration, App password generator, SMTP configuration in Laravel 7. I hope, this post will help you to migrate the Email and SMTP configuration with the Laravel applications.
Sakshi Sharma says
Thank you so much for this tutorial I was eagerly waiting for this topic.