Laravel 12 Send Email using Markdown

In this tutorial, I’ll show you how to send emails using Markdown in Laravel 12. Laravel makes it really easy to create beautiful email templates with Markdown. You don’t need to design the email from scratch — Laravel gives you pre-built layouts and components.

Laravel 12 Send Email using Markdown: Step by Step Guide

 

Step 1: Install Laravel 12

Install laravel 12 using the following command. 

laravel new laravel-email-markdown

 

Step 2: Configure Mail Settings

Open env file and set your mail configuration. If you're using Mailtrap for testing, use these settings.

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

 

Step 3: Create Mailable Class with Markdown

Run the following artisan command to generate a mailable class using Markdown:

php artisan make:mail SendDemoMail --markdown=emails.demo

This command will create a file at:

app/Mail/SendDemoMail.php
resources/views/emails/demo.blade.php

 

Step 4: Update Mailable Class

Open app/Mail/SendDemoMail.php and update it like this:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendDemoMail extends Mailable
{
    use Queueable, SerializesModels;

    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Demo Markdown Mail',
        );
    }

    public function content(): Content
    {
        return new Content(
            markdown: 'emails.demo',
        );
    }

    public function attachments(): array
    {
        return [];
    }
}

 

Step 5: Create Markdown Email Template

Open resources/views/emails/demo.blade.php and add this content:

@component('mail::message')
# Hello, {{ $data['name'] }}

This is a **demo email** using Laravel **Markdown**.

@component('mail::button', ['url' => $data['url']])
Click Here
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

 

Step 6: Create Route and Controller

Let’s create a route and controller method to send the email:

Create Controller:

php artisan make:controller MailController

Update routes/web.php

use App\Http\Controllers\MailController;

Route::get('/send-mail', [MailController::class, 'sendEmail']);

Update MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\SendDemoMail;
use Mail;

class MailController extends Controller
{
    public function sendEmail()
    {
        $data = [
            'name' => 'John Doe',
            'url' => 'https://example.com'
        ];

        Mail::to('[email protected]')->send(new SendDemoMail($data));

        return 'Email sent successfully!';
    }
}

 

Step 7: Test the Email

Now run your Laravel app:

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS