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
Install laravel 12 using the following command.
laravel new laravel-email-markdown
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}"
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
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 [];
}
}
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
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!';
}
}
Now run your Laravel app:
php artisan serve
You might also like: