Laravel 9 Markdown Mailable Send Email Example
Hello artisan,
You know that we can send email in Laravel without markdown mailable, but in this example, we will see how to send email in Laravel 9 using a markdown template. We can easily create blade file for mail templates using the markdown mail class. So in this article, I will give you an example of laravel 9 send markdown mail. I will show you a step by step guide on laravel 9 mail send markdown.
In this tutorial, I will use laravel 9 send email using markdown. It is going to be a step by step simple example of laravel 9 sending mail using mailable. Laravel 9 Markdown provides pre-define mail templates and components for email in laravel. We can use components for tables, emails, links, buttons, embed images, etc by default.
In this tutorial, I will give you step by step guide to send emails using markdown in laravel 9. You have to just follow the below few steps:
Step 1: Download Laravel 9
We need a fresh Laravel 9 project to create this tutorial, if you have already a project then you can ignore this step.
composer create-project laravel/laravel example-app
Step 2: Mail Configuration
We are going to send an email with a markdown mailable in Laravel. So we need to configure the .env
file like:
.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}"
Step 3: Create Mail
In this step, we will create a markdown mail by running the below command. So run the below command and update the file like the below:
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 a controller and we will send mail from that controller. So create a controller and update it like:
app/Http/Controllers/MailController.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 codecheef.org',
'url' => 'https://www.codecheef.org'
];
Mail::to('your_email@gmail.com')->send(new DemoMail($mailData));
dd("Email is sent successfully.");
}
}
Step 5: Update Routes
In this step, we have 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: Update Email View
We already know that Laravel Markdown email provides the default mail template in Laravel. So update it like this:
resources/views/emails/demoMail.blade.php
Now we can test by visiting the below URL:
visit
Read also: Vue Laravel CRUD Example With Vue Router and Sweet Alert
Hope it can help you.