Legendary Emails in Node js with mjml 📩

Ali nazari - Jun 30 - - Dev Community

Sometimes I receive emails from various companies and start-ups that look very attractive and audience-friendly. 😶‍🌫️

In their emails, they used a unique font along with images and buttons. What stood out the most was how their email format was responsive, adapting well to different devices.

After seeing the emails, I decided to send similar emails to users for the store project I was developing 😎

you can check it out if you like from the link below:
https://github.com/Silent-Watcher/express-shop

Anyway, I stumbled upon a tool called MJML that could be used to implement this feature.

As stated in its documentation:

MJML is a markup language designed to reduce the pain of coding a responsive email.

As developers, we don't have to be involved in complex responsive email designs but you can spend some time learning the syntax if you’d like.

Good news for Node.js developers: MJML is coded in Node.js. However, if you use other languages such as Python, you can utilize the MJML API.

For more information, you can refer to this post on the Medium website

To begin, we will need to install two packages: eta and mjml.

bun add eta@latest mjml@latest
Enter fullscreen mode Exit fullscreen mode

After that, I created a file named mail.tpl.js where we initialize MJML and ETA to create our email template.

mjml

The template I used is the hello world template from the MJML documentation, which looks something like this

mjml email preview

After accessing your email template in HTML format, if you are using *Nodemailer * for sending emails, you can create a ‘sendmail’ function that can be utilized later in the project

To achieve this, I created a file named ‘mailer.js’ and initialized the Nodemailer package:

Nodemailer

Now, you can utilize this function wherever you wish to send emails. To define your template, simply use the ‘html’ option within the first parameter.

send mail in node js

that’s pretty much it. let me know what you think. 🤗

. . . . . . .