How to Send Email in Laravel 9 with Markdown Template Example

Now, sending email in Laravel application with Markdown is no more difficult; this quick not only but also step by step guide tells you clearly how to Send Email in Laravel with Markdown template.

How does Laravel 9 send markdown email work?

There are two services you are going to use:

Mailable class: It amplifies sending an email by creating a “mailable” class which manages setting up your emails.

Markdown mailable: It empowers you to take hold of the pre-built templates and components of mail notifications in your mailable.

Since the messages are written in Markdown, Laravel handles markdown messages beautifully, transforms them into responsive HTML, and generates them in plain text.

This quick tutorial shows you an exorbitantly facile example of Laravel mail send Markdown. By the end of this tutorial, you will quickly assimilate the process of Laravel send mail using Markdown.

Install Laravel Project

Create an application with below command:

composer create-project laravel/laravel --prefer-dist laravel-markdown-demo

Adding Mail Configuration in ENV

Open .env configuration file, add the Gmail SMTP username, password and other configurations:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_email@gmail.com
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

Gmail App Settings

First, you have to turn on the 2-Step Verification, also turn on the allow less secure apps feature; you have to tweak the Gmail SMTP setting by visiting Google settings.

Allow less secure apps

Define Markdown with Mailable Class

Use the given command to create your mailable class using artisan make:mail command:

php artisan make:mail OfferMail --markdown=emails.offerSendMail

In response to the above command, a new file is generated on the following path app/Mail/OfferMail.php. Replace the entire code with the given below code example:

<?php

namespace App\Mail;

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

class OfferMail extends Mailable
{
    use Queueable, SerializesModels;
    public $offer;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($offer)
    {
        $this->offer = $offer;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->markdown('emails.offerSendMail')
            ->with('offer', $this->offer);
    }
}

Create and Organize Controller

Evoke below command to generate a new controller:

php artisan make:controller MailController

Put the below code in app/Http/Controllers/MailController.php:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Mail\OfferMail;
use Mail;


class MailController extends Controller
{
    public function sendOfferMail()
    {
        $email = 'test@gmail.com';
   
        $offer = [
            'title' => 'Deals of the Day',
            'url' => 'https://www.remotestack.io'
        ];
  
        Mail::to($email)->send(new OfferMail($offer));
   
        dd("Mail sent!");
    }
}

Import Mail service and OfferMail mailable class, create a function to handle the sending email by incorporating the email and adding mail data.

Define Route

Bind the controller with the route to make the request to send the mail with markdown, add the code in routes/web.php file.

<?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-markdown-mail', [MailController::class, 'sendOfferMail']);

Create Mail Tempalte

Open resources/views/emails/offerSendMail.blade.php view file put the mail configuration or data inside the mail template.

@component('mail::message')
# Introduction

The body of your message.

@component('mail::button', ['url' => $offer['url']])

Button Text
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

Send Markdown Mail Demo

Use below command to get rid from “Cannot send message without a sender address in Laravel” exception:

php artisan config:clear

To send the mail you have to start the app:

php artisan serve
http://127.0.0.1:8000/send-markdown-mail

Summary

Now that you can easily create the Laravel markdown mail templates at a lightning-fast speed while easily send email using Laravel mailable class.