Laravel Markdown



Laravel is one of the most popular PHP Framework. Today we are going to talk about its built in Email Notification functionality. We are using make:notification artisan command to create notification. The Notification only available on Laravel 5.3.x and above. I am using Laravel 5.7.x and hope this will works with the supported versions too.

Generate Notification

Laravel markdown command

If you haven’t set up your project yet, then follow this official documentation like to set up your project.

Creating Notification is very simple with the handy command.

How does it work? You simply create a new theme and start picking colors per section. When you are satisfied with the theme colors you can click one of the generate buttons to either generate a Laravel Markdown CSS theme or the plain generated HTML including inline styling. Markdown mailable messages allow you to take advantage of the pre-built templates and components of mail notifications in your mailables. Since the messages are written in Markdown, Laravel is able to render beautiful, responsive HTML templates for the messages while also automatically generating a plain-text counterpart.

The above command will create a folder Notifications in app directory app/Notifications/WelcomeNotification.php.

Laravel Notification supports various features like sending email, SMS, Slack etc. We are not going to discuss all of these features in this post. We are going to customize its default email template, specially header and footer.

Customize Markdown Email Header and Footer

Email notification header and footer changes with APP_NAME configuration. Find .env file in your projects root directory that has this APP_NAME parameter with the value Laravel. Lets change it to the Mailing App instead of Laravel.

If we change the APP_NAME like the above, the output of the email should be like the below image.

But this is not what we want! We want to set the Logo on the email header section and Support Team of something else on the footer.

Laravel provides a more better way to customize the email notification more robust. Let’s digging it more deeper.

The above command will publish the Markdown mail components to resources/views/vendor/mail.

Now open the file message.blade.php from resources/views/vendor/mail/html/messages.blade.php

The above file contains the header and footer slots that you can modify according to your need. If you want to set the logo on the Markdown email template then modify this block.

Replace {{ config(‘app.name’) }} with img tag and set the logo here.

But here is one thing to notice about the footer. This footer slot only modifies the very bottom section on the footer. Not the Regards one.

So what we have to do to change the Regards section? Run the following command.

The above command will generate another directory notifications on resources/views/vendor/. Now open email.blade.php from the notifications directory.

This file holds the Regards section and the Greeting section too. So if you want to set the Greeting from Hello! to Hi or something else, then you can change it from here.

So here is the Regards section that you can change according to your need.

Related posts:

  1. Create sticky footer with jquery and css
  2. Laravel Customize Default Authentication
  3. Email Validation with php and jquery
  4. Implementing Swift Mailer with Codeigniter
  5. Laravel 5.5 Pagination for Bootstrap 4

From the About section of league/commonmark package:

Laravel Markdown
  • Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.

This package helps us parse markdown in PHP. If you are not familiar with markdown, GitHub has super helpful guide about it here.

Mail in Laravel

Laravel provides simple and easy ways to send emails. There are two options in Laravel to send emails:

  • Mailables
  • Notifications

I created a poll on Twitter to see how Developers usually send emails in their Laravel apps. Based on the results, seems like Mailable is the go-to choice. We'll also use Mailable to explore further.

(Interesting replies on this tweet, give them a read)

How do you usually send emails in a Laravel app? 👀#Laravel#PHP

Markdown— Zubair Mohsin (@Zubairmohsin33) March 15, 2021

Markdown Mailables

Let's generate a markdown mailable using Artisan.

We get two files as a result of this command.

  • AppMailNewsletterSubscribed class
  • resources/views/emails/newsletter/subscribed.blade.php file
Markdown

Let's take a look at view file:

We can see that there's a combination of Blade Components and Markdown in this file. These components and others are made available by Laravel, read more about components here. When we send out this mailable, league/commonmark package comes into play and parse this markdown to HTML.

What happens when you Mail::to()→send() ?

We are specifying the recipient in to() method and the mailable class in send() method.

Code Dive

Let's dive into send() method. It leads us to IlluminateMailPendingMail class.

Mailable class is being filled with addresses and then its calling send method on the given Mailer instance. As we can see, $mailer is an instance of class which implements the Mailer contract. Where do we find its concrete class implementation?

Finding the Mailer

When request comes in and Laravel registers the ServiceProviders, part of these providers is IllumiateMailMailServiceProvider. Let's take a look at its register method.

It is binding a singleton of MailManager class and then bindMailer by calling mailer() method on mail.manager singleton above.

  • We can already see a Markdown class being registered. We will eventually reach to this class.
  • Read more about singleton() and bind() binding methods in the documentation

Let's dig into mailer() method of MailManager class.

Above code can be roughly translated to:

  • Get the default mail driver / default mailer (which is smtp) then call get() method on it. get() method checks if given mailer has already been resolved ( local cache ), otherwise resolve the concrete class for the given mailer.

We found the Mailer concrete class. Yay!

It's located at IlluminateMailMailer. It was kind of obvious, but finding it through code-dive was fun. Alright, moving on...

The Mailer

We are interested in send() method on the Mailer class. Let's take a look:

In the send() method it checks if $view is an instance of Mailable , in our case this is true. We are indeed working with Mailable and we passed a mailable from PendingMail class.

Then it sets Mailer on Mailable class itself and then calls send method on the Mailable.

The Mailable

If we were to find concrete class that implements Mailable class, this post will become huge.

Therefore, we are just going to assume that IlluminateMailMailable class is what we need as our NewsletterSubscribed class extends it and it also implements IlluminateContractsMailMailable interface.

In context of our topic, buildView() method is of our interest. Below is its implementation and related methods.

buildMarkdownView() method is where it is initialising Markdown class from container and rendering the markdown view of our NewlsetterSubscribed mail.

The Markdown

In the Markdown class, there are two important methods. render and parse.

And we can finally see inside the parse() method that it utilizes classes from league/commonmark package like CommonMarkConverter and TableExtension etc.

But we never called parse() method from anywhere? And we can see nothing inside the render() method related to markdown either?

Laravel Markdown Free

Well, parse() method is called from within the email views. Our subscribed.blade.php view uses @component('mail::message') which in-turn uses @component('mail::layout') , and if we take a look at layout component at Mailresourcesviewshtmllayout.blade.php , we see an HTML template with table layout and some styling.

Laravel Markdown Mail

There we see the following code in which parse() method is being called.

When the subscribed view is rendered, parse gets called and our markdown content is parsed using league/commonmark package.

Interesting facts about Markdown in Laravel

  • Laravel started supporting Markdown syntax in emails **in version 5.4**
  • The first package used to parse markdown was erusev/parsedown
  • In Laravel v6.0, they switched from erusev/parsedown to league/commonmark for safety features.
  • Taylor Otwell removed erusev/parsedown from composer.json and added league/commonmark on 30 Dec, 2019.
  • Commit on GitHub can be found on this link

Laravel Markdown Download

I hope you enjoyed this post. Next, we will see how Laravel uses league/flysystem package. You can follow me on Twitter or join my newsletter to keep yourself updated.