Sending an email is not a difficult task in Laravel 6. In comparison to the normal PHP script, Laravel provides libraries and classes. Laravel has prefaced the Mailable class for sending emails. There are some API based drivers for sending emails in the Laravel 6. These are much faster and secure to send emails. Today, I am going to show you how to send email in Laravel 6 using the Gmail account. For this, we’ll have to configure the Gmail SMTP connection. The email will send by the mailable class. In the previous post, I have already shown you to create RESTful APIs in Laravel 6 with passport authentication.
Prerequisites
For creating this project your system must be ready for Laravel 6. Also, you will have to install the PHP (version >=7.3.9) and the MySQL (version > 5).
Send Email in Laravel 6
For the email setup in the Laravel, let’s create a new project first. For creating a new project you will need to enter the below command in the terminal or in the command prompt window.
composer create-project --prefer-dist laravel/laravel send-email
It will take some time for creating the project.
Once the project has been created successfully, just open it in the editor. Now, we will configure the database and email details.
Laravel 6 REST API For ToDo Application with Passport Auth
Configure Database
Laravel 6 doesn’t allow you to run the project without the configuration of the database. In other words, you will have to put the correct database details in the .env file. If you leave it as by default then you won’t be able to run the project.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel6_email
DB_USERNAME=root
DB_PASSWORD=root
We are not going to use the database further in this project. It is just necessary for running the application.
Configure Gmail Account For Sending Laravel Email
Firstly, you will have to allow the less secure app access in your Gmail account. You will find it inside the Security tab.
Secondly, set the two-step verification method for your Gmail account. This is just for the security purpose of your Gmail account.
Lastly, you will have to generate an app password. Gmail won’t validate the account credentials directly if you put the login details in the SMTP configuration. So you will have to create the app password of your Gmail account. This is the way to proceed securely.
Under the Security tab, you will find the App password option. So just generate it as shown below. It will be of 16 digits so just copy it.
Now, we will paste it in our mail configuration file (.env file).
How to Integrate Laravel 6 Application with Firebase
Configure SMTP to Send Email in Laravel
The SMTP provides the features for sending the Laravel email. Here, you will need to put the mail details. The default mail driver, host, port, etc are defined below.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=youremail@gmail.com
MAIL_PASSWORD=your-app-password
MAIL_ENCRYPTION=tls
Just put your email and app password here. For security purposes, Gmail will not allow putting the regular password as open characters.
Create Mail in Laravel 6
Laravel artisan command provides the feature to create the Mail helper class. It is used to send the email in the Laravel.
php artisan make:mail SendEmail
The above command has created a file with the name SendEmail.php inside the app/Mail directory.
Now, add the following lines of code inside the SendEmail.php file.
RESTful APIs in Laravel 6 with Passport Authentication
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendEmail 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 added a from email along with the view name. This view will need to be created. So, just create it.
Create Email Template(View)
Navigate to the resources/views and create a folder with a name email. Then inside this folder create a blade file with name mail-template.blade.php. Actually, we will attach this email template with the Laravel email.
Then add the below lines of HTML just for a basic email template.
<p>Hello</p>
<p>Welcome to Programming Fields</p>
<p>This is an example of SMTP email in Laravel 6. </p>
<br/>
<br/>
<p>Regards </p>
<p>Team programming fields </p>
Create a Controller to Send Email in Laravel 6
For sending the Laravel email we will need to create a controller file. As we know that we cannot call the Views directly in the MVC. So move to the terminal and enter the below command.
php artisan make:controller UserController
The above command will create a controller named UserController. So for sending the Laravel email, we will use this controller.
Paste the below snippet in the UserController.php file.
// UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendEmail;
class UserController extends Controller
{
public function sendEmailToUser() {
$to_email = "rajumesh52@gmail.com";
Mail::to($to_email)->send(new SendEmail);
return "<p> Your E-mail has been sent successfully. </p>";
}
}
In the above code, I have created a function. Inside the function declared a variable and passed a static email. You can do it dynamically using the form. You will need to read the email from the Form input.
Then using the Mail facades, I have called the send() function for sending the email. At last, I returned a message as a response to successfully sent the email.
Laravel 6 Login and Registration with Authentication
Add Route For Controller
Move to the routes/web.php file and paste the below route there.
Route::get('send-mail','UserController@sendEmailToUser');
Once you have done with the above steps, just run your project.
php artisan serve
If you have done the above steps correctly, then you will get the result as shown below.
You can check your Gmail account.
Bingo! you can see here, I have received the mail here.
How to Use Yajra Datatables in Laravel 6
Conclusion
So, we achieved the Laravel send email using SMTP. This was just for a basic demonstration of sending an email in the Laravel 6. You can create a more attractive Email template. Also, you can create a form with a sender email, subject, and message also. In the upcoming post, I will publish a more advanced post on this. Hope, you will enjoy this post. If you find any difficulty while sending the email in Laravel 6 then please let me know by doing comment here. I will definitely help you. Thank you.
Leave a Reply