How to Send Email Using Laravel with Mailable Markdown Class

In this post, you’ll learn how to create markdown templates for sending email in laravel.

What is Markdown in Laravel Mailables?

Creating good looking attractive emails templates are not easy. As you need to write inline and internal CSS in HTML file which makes it difficult for making any changes and also you cannot attach any external CSS file.
And if you think you can link an external CSS file to your then visit this thread on link CSS file from an email which will answer all your questions.

First of all, Markdown refers to the conversion of text-based mnemonic format into HTML format which can be rendered into the browser. If you look and popular Q&A discussion forms like stack overflow, dev.to you observe that these forums also heavily use markdown syntax into there editor.

There are also third party markdown editors which you can use to integrate it into your own project.

Common Syntax of Markdown

# Heading 1: This will display content inside h1 tag

## Heading 2: This will display content inside h2 tag

### Heading 3: This will display content inside h3 tag

Using these above simple symbols you can create an email template using laravel markdown emails. This will eliminate excess HTML tags in the document.

Why use Laravel Markdown Mailables?

  • Laravel Markdown emails are easy and simple to install and configure.
  • Laravel provides you with pre-built components which you can use out of the box.
  • You can also customize existing components to your own needs.
  • You can also create your own component from scratch.
  • Components or HTML tags generated from markdown are mobile responsive. So that you don’t need to spend the excess time on design and its responsiveness.

Table of Contents

Generating Markdowns for Email

For sake of demonstration let us create an email template for the remainder for renewal of annual subscription plans. So the customer can choose a particular subscription which will be redirected them to website.

For creating mailable with markdown use below command.

php artisan make:mail SubscriptionRenewal  --markdown=emails.subscription-renewal

--markdown the parameter specifies the path where the view of markdown email must be created. In this case, it is created at the path laravel_app/resources/views/emails/subscription-renewal.blade.php.

The subscription-renewal.blade.php the file is already prepopulated with markdown components.

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

The body of your message.

@component('mail::button', ['url' => ''])
Button Text
@endcomponent

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

For previewing emails onto the browser. Add below snippet into the controller.

public function emailOfSubscriptionRenewal(){
    return (new SubscriptionRenewal());
}

Add route for accessing rendered email template.

Route::get('email/email-of-subscription-renewal', 'HomeController@emailOfSubscriptionRenewal');

If you navigate to email/email-of-subscription-renewal in the browser, you will see the template rendered. The rendered email template looks good and also made responsive for mobile devices.
default look of newly created markdown template view in Laravel email

Pre-built components for Markdown email

In Laravel’s markdown section documentation has explained some of the pre-built components which you could use to create good looking emails.
Some of those components are explained below.

@component('mail::message')

# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6

This is a sentence

This is a sentence with **bolded text**.

@component('mail::button', ['url' => '', 'color' => 'success'])
Button One
@endcomponent

@component('mail::button', ['url' => '', 'color' => 'primary'])
Button Two
@endcomponent

@component('mail::panel')
This is the panel content.
@endcomponent

@component('mail::table')
| Items             | Quantity  | Total Price  |
| ------------------|:---------:|-------------:|
| Apple Iphone 10x  | 1 Nos     | $1200.00     |
| Backcase cover    | 2 Nos     | $15.00       |
| Total             | 3 Nos     | $1215.00     |
@endcomponent

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

laravel markdown email pre-built components

For learning more about markdowns you can go through an amazing guide for the basic syntax of markdowns which will help you to make use of its hidden functionalities.

Caution

More indentation while writing markdowns will create problems in markdown parser for properly rendering HTML. So don’t add excess indentation to your markdown.

Customizing components for Markdown email

If you wish to customize pre-built components then use below command.

php artisan vendor:publish --tag=laravel-mail

vendor:publish --tag=laravel-mail will copy assets and components to path laravel_app/resources/views/vendor. You can customize your components in this location.

Below is a customized snippet for subscription remainder email created using markdown.

@component('mail::layout')
{{-- Header --}}

@slot('header')
@component('mail::header', ['url' => config('app.url')])
The Code Learner
@endcomponent
@endslot

# Renewal Subscription Plan

##### **Dear Customer**,
A gentle reminder for informing you about your annual subscription plan coming to end.

### You can choose Plans from below.

@component('mail::table')
| Single Plan   | Premium Plan  | Business Plan |
| ------------- |:-------------:| -------------:|
| Feature 1     | Feature 1     | Feature 1     |
| Feature 2     | Feature 2     | Feature 2     |
| Purchase     | Purchase     | Purchase    |
@endcomponent

@component('mail::button', ['url' => '', 'color' => 'primary'])
Go to Account
@endcomponent

{{-- Footer --}}
@slot('footer')
@component('mail::footer')
© {{ date('Y') }} The Code Learners. @lang('All rights reserved.')
@endcomponent
@endslot
@endcomponent

Laravel customized markdown email

Conclusion

You have come to end of How to Send Email Using Laravel with Mailable Markdown Class post. Support us by sharing this post which will help us grow and comment if you have any doubts we will reach you soon.

You can also read article on Laravel sending email and also emailing with attachment.