Laravel 9 Markdown Send Email using Markdown Mailables

Hello Artisan,

In this tutorial, I will show you how to create and send markdown mail in Laravel 9 application. So from this tutorial, you will learn how to send mail using a markdown template. You know that laravel provides many mail driver support.

What is the advantage of the markdown template in Laravel 9? Actually, Markdown provides inbuilt pre-define mail templates and components for email. We can use components for tables, email, links, buttons, embed images, etc and we can customize it very easily.

So if you don't know how to send email using markdown in Laravel 9, then this laravel 9 markdown email example tutorial is for you.So let's see and follow the below step by step instructions to send emails using markdown in laravel 9.

 

Step 1: Install Laravel 9

I am going to start this tutorial from scratch. So download a fresh Laravel 9 application to send email in Laravel.

composer create-project laravel/laravel example-app

 

Step 2: Make Configuration

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9 will use those sender configurations for sending an email. So you can simply add as like following.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"

 

Read also: How to Send Email in Laravel 8.x with Mailtrap

 

Step 3: Create Mailable Class with Markdown Template

In this step, we will create a mail class MyDemoMail for email sending. Here we will write code for which view will call and object of user. So let's run the below command.

php artisan make:mail MyDemoMail --markdown=emails.myDemoMail

 

now, let's update the code on the MyDemoMail.php file as below:

app/Mail/MyDemoMail.php

namespace App\Mail;
  
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
  
class MyDemoMail extends Mailable
{
    use Queueable, SerializesModels;
  
    public $mailData;
  
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($mailData)
    {
        $this->mailData = $mailData;
    }
  
    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('Mail from codecheef.org')
                    ->markdown('emails.myDemoMail');
    }
}

 

Step 4: Create Controller

In this step, we will create MailController with index() method where we write code for sending mail to a given email address. so first let's create a controller by following command and updating code on it.

app/Http/Controllers/MailController.php

namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use Mail;
use App\Mail\MyDemoMail;
  
class MailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from codecheef.org',
            'url' => 'https://www.codecheef.org'
        ];
         
        Mail::to('to_your_email@gmail.com')->send(new MyDemoMail($mailData));
         
        dd("Email is sent successfully.");
    }
}

 

Step 5: Create Routes

In this step, we need to create routes for a list of sending emails. so open your "routes/web.php" file and add the following route.

routes/web.php

use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\MailController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('send-mail', [MailController::class, 'index']);

 

Step 6: Create Blade File

In this step, we will create a blade view file and write an email that we want to send. now we just write some dummy text. create bellow files on the "emails" folder.

resources/views/emails/demoMail.blade.php

 

Now all are set to go to test our mail sending using markdown in Laravel 9 application. Run php artisan serve command to test it. 

 

Read also: Send Mail to Inactive User with Cron Jobs in Laravel

 

Hope this Laravel 9 markdown email example will help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js