Hello developers! In this article, we'll see how to send mail using the markdown template in laravel 10. Here, we'll use laravel markdown email to send emails. Markdown mailable messages allow you to take advantage of the pre-built templates and components of mail notifications in your mailables.
Laravel is able to render beautiful, responsive HTML templates for messages while also automatically generating a plain-text counterpart.
To generate a mailable with a corresponding Markdown template, you may use the --markdown
option of the make:mail
Artisan command
In this guide, I'll walk you through the simple steps of sending emails using Markdown templates in Laravel 10.
If you haven't already, start by creating a new Laravel project using Composer:
composer create-project laravel/laravel your-project-name
cd your-project-name
Configure mail configuration in the .env file.
MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=your_password
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
Run the following Artisan command to generate a new Mailable class.
php artisan make:mail TestMail --markdown=emails.testMail
After running the command, navigate to the app/Mail
directory in your Laravel project.
app/Mail/TestMail.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class TestMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
public function __construct($details)
{
$this->details = $details;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'This mail from Vidvatek',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
markdown: 'emails.testMail',
);
}
/**
* Get the attachments for the message.
*
* @return array
*/
public function attachments(): array
{
return [];
}
}
Next, we'll create a controller using the following command.
php artisan make:controller TestMailController
app/Http/Controllers/TestMailController.php
<?php
namespace App\Http\Controllers;
use App\Mail\TestMail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class TestMailController extends Controller
{
public function sendMail()
{
$details = [
'title' => 'This Mail from Vidvatek',
'url' => 'https://www.vidvatek.com'
];
// Customize the logic for sending the email
$recipient = '[email protected]';
// Send the email using the Mailable class
Mail::to($recipient)->send(new TestMail($details));
return "Test email sent to $recipient";
}
}
Open the routes/web.php
file and define a route.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestMailController;
/*
|--------------------------------------------------------------------------
| 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', [TestMailController::class, 'sendMail']);
The Markdown template associated with this Mailable class is located at resources/views/emails/testMail.blade.php
.
resources/views/emails/testMail.blade.php
@component('mail::message')
# Hello! {{ $details['title'] }}
This is a test email with Markdown.
@component('mail::button', ['url' => $details['url']])
Visit Our Website
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
Run the following command to start the Laravel development server
php artisan serve
You might also like:
- Read Also: How to Send Email with PDF Attachment in Laravel 10
- Read Also: Laravel 10 Two Factor Authentication Using Email
- Read Also: How to Send Bulk Mail using Queue in Laravel 10
- Read Also: Laravel 10 Send Mail Using Queue