How to Send Email in Laravel 8 with Mailable and Mailtrap

In this tutorial you will learn about the How to Send Email in Laravel 8 with Mailable and Mailtrap and its application with practical example.

How to Send Email in Laravel 8 with Mailable and Mailtrap

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

Laravel 8 provides builtin mailable markdown class to send emails. In Laravel 8 you can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail for sending emails. You are required to configure driver details on the .env file.

In this step by step tutorial you will understand how to send emails in Laravel 8 with the help of Mailtrap. We will explain to you how to use Laravel 8 builtin mailable markdown class to send emails using Mailtrap.

Install Laravel 8

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

Make sure you have composer installed.

Setup Laravel 8 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

Before you start sending emails using mailtrap from your laravel 8 application, you are you need a mailtrap configuration to configure SMTP settings in your laravel project. Here is how you can get the required credentials from mailtrap.

  • Create an account using GitHub or Email at mailtrap.io
  • Login to your mailtrap account
  • Create inbox project
  • Click on the settings icon
  • Go to the SMTP/POP3 tab
  • Select Laravel from the Integrations dropdown
  • Copy the Laravel mailer details

How to Send Email in Laravel 8 with Mailable and Mailtrap

Now, open .env file within your application’s root directory and set up mailtrap configuration as following:

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 8 using Mailtrap

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

In this tutorial we have learn about the How to Send Email in Laravel 8 with Mailable and Mailtrap and its application with practical example. I hope you will like this tutorial.