How to Send Email by using Markdown Mailables in Laravel 9?

Today now in this article i will give you an example of laravel 9 send mail by using markdown mail. Here I would like to show you an example of laravel 9 mail send markdown. Here we will use the laravel 9 send email by using markdown. It’s a very simple example of laravel 9 sending mail by using mailable.

We know that Laravel 9 Markdown is provides the inbuilt pre-define mail templates and also components for email. We can use the components for tables, email, links, buttons, embed images, etc.

Now in this tutorial, I will give you all step by step instructions about how to send emails by using markdown in laravel 9. So, just need to follow the bellow few steps and make it done.

Step 1: Install Laravel 9

This is the first step. This step is not need if you already install laravel 9 in previous. however, if you don’t have then need to created the laravel application, So youneed to go ahead and then execute the below command:

composer create-project laravel/laravel example-app
Step 2: Make Configuration

Now in first step, you need to add send mail configuration also with mail driver, mail host, mail port, mail username, mail password. So in laravel 9 we will use those sender configuration for sending email. So we can simply add as like as following.

.env

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=mygoogle@gmail.com
MAIL_PASSWORD=rrnnucvnqlbsl
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mygoogle@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Step 3: Create Mailable Class with Markdown

Now in this step we need to create the mail class MyDemoMail for email sending. Here i can write code for which view will call and object of user. So let’s run the bellow command.

php artisan make:mail MyDemoMail --markdown=emails.myDemoMail

now, let’s need to update code on MyDemoMail.php file as like as bellow:

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 ItSolutionStuff.com')
                    ->markdown('emails.myDemoMail');
    }
}
Step 4: Create Controller

Now in this step, i will create a controller as name MailController with index() method where we can write the code for sending mail to given the email address. So at first let’s create the controller by using the following command and then update code on it.

php artisan make:controller MailController

Now, need update code on MailController file.

app/Http/Controllers/MailController.php

<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
use Mail;
use App\Mail\MyDemoMail;
  
class MailController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $mailData = [
            'title' => 'Mail from ItSolutionStuff.com',
            'url' => 'https://www.itsolutionstuff.com'
        ];
         
        Mail::to('to_your_email@gmail.com')->send(new MyDemoMail($mailData));
         
        dd("Email is sent successfully.");
    }
}

Step 5: Create Routes

So in this step, we have to create the routes for list of sending email. Now need to open our “routes/web.php” file and then add 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: Create Blade View

So in this step,i will create a blade view file and then write the email that i want to send. Now here i just write the some dummy text. For create the bellow files on “emails” folder.

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
Run Laravel App:

That’s good all required steps have been completed , now we need to type the given below command and then hit enter to run the Laravel application:

php artisan serve

Read Also: How To Implement Laravel 9 form validation ?

Thanks for read. I hope it help you. For more you can follow us on facebook

close

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →