Laravel 9 Send Email Example Tutorial

In this tutorial you will learn about the Laravel 9 Send Email Example Tutorial and its application with practical example.

In this Laravel 9 Send Email Example Tutorial I will show you how to send email in laravel 9 using mailable and mailtrap. We will use Laravel 9 builtin mailable markdown class to send emails. In order to send email using mailtrap we will implement laravel 9 builtin mailable markdown class. In this post I’ll show you how to integrate mailtrap to send email in laravel 9 application.

Laravel 9 Send Email Example Tutorial

In this step by step tutorial you will understand how to send emails in Laravel 9 with the help of using mailable and mailtrap. I will also demonstrate you how to use Laravel 9 builtin mailable markdown class to send emails using Mailtrap. Please follow the instruction given below:

  • Install Laravel 9 App
  • Setup Database Credentials
  • Configuration SMTP in .env
  • Create Mailable Class
  • Create Email Controller
  • Add Email Send Route
  • Create Directory And Blade View
  • Run Development Server

Install Laravel 9

First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command

Make sure you have composer installed.

Setup Database Credentials

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Setting Up Mailtrap Mail Configuration

In this step we will be setting smtp detail for mail configuration in Laravel, We will be using following Gmail SMTP details such as username, password inside the .env file.

If we are sending email from localhost using Gmail SMTP, then it requires you to turn on the “Allow less secure apps” on?. Go to the link provided:

Now, You need to turn on the option “Allow less secure apps”

Create Laravel Markdown Mailable Class

The mailable class in Laravel used for sending emails. Now, lets create a new laravel mailable class using following artisan command:

Once the above command executed, it will create a new laravel mailable class DemoEmail.php in app/Mail/ directory. Open the DemoEmail.php file and put the following code in it.

Create Controller to Send and Email

Now, we will create a controller that will handle the logic to send email. Let’s create DemoEmailController Controller using following command:

Once the above command executed, it will create a controller file DemoEmailController.php in app/Http/Controllers/ directory. Open the DemoEmailController.php file and put the following code in it.

Controllers/DemoEmailController.php

In the DemoEmailController.php we have included DemoEmail Mailable class along with the Mail Facade and Http Response service. Later we have created controller method to send demo email from our laravel 8 application.

Create Route to Send Mail

After this, we need to add following route in “routes/web.php” to send email. Lets open “routes/web.php” file and add following route.

routes/web.php

Create Email Template

In this step, we will create view/blade file to setup email template. Open resources/views/Email/demoEmail.blade.php file, put the following code.

resources/views/Email/demoEmail.blade.php

Start Development Server

Now we are ready to run our example so lets start the development server using following artisan command:

Send Email in Laravel

Now, open the following URL in browser to send email:

Output:-

How to Send Email in Laravel 9 using Mailtrap

When you visit the link, you will receive the example email in your inbox.

In this tutorial we have learn about the Laravel 9 Send Email Example Tutorial and its application with practical example. I hope you will like this tutorial.