In this Laravel 9 markdown mail template tutorial, I will give you a complete step by step example of Laravel 9 send markdown mail. I will show you Laravel 9 mail send markdown. I will use Laravel 9 send email using markdown template. It is going to be a simple example of Laravel 9 sending mail using mailable.
Laravel 9 Markdown email template provides inbuilt pre-define mail templates and components for email. You can use components for tables, email, links, buttons, embed images, etc in Laravel markdown mail template.
Step 1: Install Laravel 9
If you do not have the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Make Configuration
In the first step, before sending markdown mail, we need to configure mail like mail host, mail port, mail username, and mail password so Laravel 9 will use that sender configuration 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=your_mail_address
MAIL_PASSWORD=your_mail_password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Read also: How To Send Mail In Laravel 9 Application
Step 3: Create Mail
There is an artisan command to create a markdown mail class with a template in Laravel. We can run make:mail
command to create a mail class. Run the below command to create a mail class.
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
<?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 laravelia.com')
->markdown('emails.myDemoMail');
}
}
Step 4: Create Controller
In this step, we will create MailController with the index() method where we write code for sending markdown mail in Laravel 9 to the given email address. so first let's create a controller by following the command and updating the code on it.
php artisan make:controller MailController
Now, update the code on the MailController file.
app/Http/Controllers/MailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Mail\DemoMail;
class MailController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$mailData = [
'title' => 'Mail from laraveia.com',
'url' => 'https://www.laravelia.com'
];
Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
Step 5: Add 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
<?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: Update Email View
Markdown email provides the default mail template in Laravel. So update it like this:
resources/views/emails/demoMail.blade.php
@component('mail::message')
# {{ $mailData['title'] }}
The body of your message.
@component('mail::button', ['url' => $mailData['url']])
Visit Our Website
@endcomponent
Thanks,
{{ config('app.name') }}
@endcomponent
Now you can test by visiting the below URL:
url
Read also: Laravel 9 CRUD Tutorial Example Using Eloquent ORM
Hope it can help you.