Creating HTTP Email, Markdown, & Notification — Laravel

Image for post
Image for post
Photo by Torsten Dettlaff from Pexels

ello there! Before we start, this article is a continuation from the previous article here. If you haven’t seen that article, don’t worry that’s very ok and you can refer to this source code of the previous article. For the recap, in the previous article, we created an email verification system when a new user registers to our app using laravel/ui packages and ui vue scaffolding. In this article, we’ll create a custom Email HTTP, Markdown, and Notification so that you can have a fully customizable email content. Ok, let’s start.

Content Overview

  • HTTP vs Markdown vs Notification
  • Configuring the Mails
  • Configuring the Mail HTTP
  • Configuring the Mail Markdown
  • Configuring the Notification

HTTP vs Markdown vs Notification

In Laravel, you can create 3 types of mails, and those are HTTP, markdown, and notification. There is a raw type email too, but I don’t think this raw type email is applicable in real-life projects.

HTTP Email is, like literally the name, an email where the content is in the HTTP syntax, and you can embed your own CSS/bootstrap/image/etc inside it. You can easily modify the content inside your HTTP syntax, and of course, you can create a button or call-to-action too. We can use php artisan to make an HTTP email.

php artisan make:mail CustomHttpEmail

What is the meaning markdown mail? Well, of course, it is a mail but, the content inside it is not HTML syntax. The good news when using this markdown is, you can type the components that you want, for example, a heading, list, or button and your view will automatically become beautiful. It is because when we are using the markdown, Laravel will automatically generate CSS to make our markdown mail beautiful. It’s super useful and saves time for us to focus on the logic, not the beauty of the mail. We can create the markdown mail with this command:

php artisan make:mail CustomMarkdownEmail --markdown=emails.custom-markdown

Last thing, what is notification? Yeah, it’s a type of mails too really. It’s kind of tricky when choosing whether we send a customer with mail or notification. You can see the differences between them in this forum. You can create the notification with this command:

php artisan make:notification CustomNotification

Configuring the Mails

Ok, let’s set up the project. In this project, we’ll continue from the previous project code, and you can see the previous article here. Let’s create those 3 types of mails and notification first.

php artisan make:mail CustomHttpEmail
php artisan make:mail CustomMarkdownEmail --markdown=emails.custom-markdown
php artisan make:notification CustomNotification

There will be 3 new files appear in your app directory.

Image for post
Image for post

Also, make sure you have resources/views directory for your mails (custom-http.blade.php & custom-markdown.blade.php). If you don’t have these files, you can manually create them.

Image for post
Image for post

Then, for the sake of simplicity, let’s create a new controller to handle all these mails.

php artisan make:controller SendEmailController

And add some new routes in routes/web.php.

routes/web.php

We’ve added some new routes, but how we can access that? Ok, let’s make some simple adjustments in your resources/views/layouts/app.blade.php.

resources/views/layouts/app.blade.php

The preparation is done! Let’s implement our mails!

Configuring the Http Mail

First thing first, let’s set up the view for the Http Mail. Open the resources/views/emails/custom-http.blade.php

resources/views/emails/custom-http.blade.php

The view above is the HTML version of what the customers will see when we send them through the email. You can customize it as you want, but for now, it’s just as simple as it is. If you take a look at the syntax, you can see a variable {{$name}} embed in the HTML and let’s make this variable work by configuring the app/Mail/CustomHttpMail.php.

app/Mail/CustomHttpMail.php

There is a constructor in the CustomHttpMail above that accept a variable. It means that, when we access this class, we must pass a variable for this constructor.

The build() function is when your HTTP view is being rendered. The exciting part is, all of your public variables inside this CustomHttpMail is automatically can be accessed by your view so that in your HTTP view, you can call {{$variable}} to access the public variable inside the CustomhttpMail.

Last thing to do, let’s set up the SendEmailController.

app/Http/Controllers/SendEmailController.php

Then it’s done for setting up the HTTP Mail! Let’s run your local server and make sure you have logged in.

Image for post
Image for post
Image 1. Home page

In the header of your home page, you can see some new buttons there. Click on the Send Http Mail to send the HTTP mail we just built. If you remember the Mailtrap from the previous article, let’s open your Mailtrap, and you’ll see a new incoming email.

Image for post
Image for post
Image 2. HTTP Mail

Well, the design isn’t that good because I’m not a good designer but, if you see the HTML Source:

Image for post
Image for post
Image 3. HTML Source

All of our HTML/CSS syntaxes is in there. Great!

Configuring the Markdown Mail

I’m not a front-end designer (as you can see in the above HTML code), so it’ll take some time for me to design a beautiful HTML page. Luckily, Laravel has a markdown mail where we don’t focus in the styling the HTML page. We focus on the components that we’ll build and let the Laravel do the rest to automatically beautifying and styling our components. Let’s implement that!

First, open and modify the custom-markdown view.

resources/views/emails/custom-markdown.blade.php

In the markdown view, we can add the components that we want to use. Laravel has several necessary components, such as a button, footer, header, layout, message, panel, promotion, sub-copy, and table. You can refer to this reference.

Then let’s modify the app/Mail/CustomMarkdownMail.php and app/Http/Controllers/SendEmailController.php.

app/Mail/CustomMarkdownMail.php
app/Http/Controllers/SendEmailController.php

The logic is the same when we are sending the HTTP mail. In fact, the only difference is in the build() function when we are returning the markdown view. So let’s try to send our first markdown mail by clicking the button “Send markdown Mail” (Image 1) and check your Mailtrap.

Image for post
Image for post
Image 4. Markdown Mail

See? The email is just beautiful even we didn’t do anything about styling. If you look at the HTML Source, you can find the CSS and many things added automatically by Laravel. Cool right.

Configuring the Notification Mail

It’s time now to implement the notification mail. Let’s modify the app/Notifications/CustomNotification.php.

app/Notifications/CustomNotification.php

With the notification, we can send the mail via a various channel at once in the via() function. For example, we can send the notification via SMS, Whatsapp, Slack, etc and it’s very very useful.

At the toMail() function, you can see that we can add and configure various components such as the subject of the email, greetings, paragraph, and so on. Interestingly, we can add a markdown view in the notification! Interesting right! Of course, it’ll be a long article if I explain it to you now but, you can refer to this documentation.

Last step, let’s modify the SendEmailController.

app/Http/Controllers/SendEmailController.php

Ok, now we can test it. Log in to your account and click the “Send Notification Mail” (Image 1) in your home page. Then, a new incoming email will appear in your Mailtrap inbox. Let’s see.

Image for post
Image for post
Image 5. Notification Email

Wow, the email is just beautiful. Again, we didn’t need to do anything with styling. Laravel will do the rest.

This article is just a simple demo to introduce the basic concept of sending an email with several ways/techniques. Of course, you can try it with another channel, for example, SMS/WA or other channels. But, the good point is, Laravel has excellent libraries/packages that makes this email system running seamlessly. So, it’ll save much time when building something with Laravel.

I hope this article can be your reference and thank you for your time :)

Github:
https://github.com/Cerwyn/Email-Verification-Laravel-UI/tree/Custom-Mail-and-Notification

PHP/Backend Engineer at Undercurrent Capital Pte Ltd — Data Science Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store