How to Send Mail using Markdown in Laravel 9?

Feb 17, 2022 . Admin


Hi all,

In this tute, we will discuss how to send mail using markdown in laravel9?. This post will give you a simple example of sending mail markdown mailables. let’s discuss about laravel 9 send mail with link and message. In this tutorial, laravel 9 sends mail using markdown. This post will give you a simple example of step-by-step send mail using markdown component in laravel 9.

Laravel Markdown provides components, tables, email link, button, embed image etc. Markdown beautiful layout you can use with email template.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

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

In the first step, you have to add send mail configuration with mail driver, mail host, mail port, mail username, mail password so laravel 9 will use those sender configurations for sending the email. So you can simply add as like following.

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

In this step, we will create a mail class SendMail for email sending. Here we will write code for which view will call and object of user. So let's run bellow the command.

php artisan make:mail SendMail --markdown=emails.demoMail

now, let's update code on SendMail.php file as bellow:

app/Mail/SendMail.php
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendMail 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 Mywebtuts.com')
                    ->markdown('emails.demoMail');
    }
}
Step 4: Create Controller

In this step, we will create SendMailController with index() method where we write code for sending mail to given email address. so first let's create controller by following command and update code on it.

php artisan make:controller SendMailController

Now, update code on SendMailController file.

app/Http/Controllers/SendMailController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\SendMail;
use Mail;

class SendMailController extends Controller
{   
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {   
        $mailData = [
            'title' => 'Send mail from Mywebtuts.com',
            'url' => 'https://www.mywebtuts.com'
        ];

        Mail::to('xyz@gmail.com')->send(new SendMail($mailData));
           
        dd("Email is sent successfully.");
    }
}
Step 5: Create Routes

In this step, we need to create routes for list of sending email. so open your "routes/web.php" file and add following route.

routes/web.php
<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\SendMailController;
  
/*
|--------------------------------------------------------------------------
| 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', [SendMailController::class, 'index']);
Step 6: Create Blade View

In this step, we will create blade view file and write email that we want to send. now we just write some dummy text. create 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:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://localhost:8000/send-mail
Output:

I hope it can help you...

#Laravel 9