DEV Community

Cover image for Legendary Emails in Node js with mjml ๐Ÿ“ฉ
Ali nazari
Ali nazari

Posted on

Legendary Emails in Node js with mjml ๐Ÿ“ฉ

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. ๐Ÿค—

Top comments (16)

Collapse
 
silentwatcher_95 profile image
Ali nazari

by the way the MJML stands for *mailjet markup language * ๐Ÿšถ๐Ÿปโ€โ™‚๏ธ

Collapse
 
jyoung4242 profile image
Justin Young

i'll file this away for my next project... thanks for the article

Collapse
 
alexswensen profile image
Alexander Swensen

I've used MJML in the past. It's effectively my go-to for beautiful emails both for campaigns and transactional emails (ie password resets, verification, etc)

Collapse
 
silentwatcher_95 profile image
Ali nazari

That's great to hear! MJML is definitely a powerful tool for creating visually appealing emails.

Collapse
 
ajmalleonard profile image
Ajmal Leonard

I think this is awesome, man. Can anyone write a post outlining how to use SMTPS (custom domain emails with Nodemailer)?
Kindly for anyone who achieved this show up , I'll appreciate ๐Ÿฆพ

Collapse
 
george_taschina profile image
George Cosmin Taschina • Edited

Hi,
thank you for sharing and writing this article.

I was just curios, why not use some mail template providers to begin with?
Like SendGrid or MailJet

Thanks again!

Collapse
 
neurabot profile image
Neurabot

Impressive. Innovant.

Collapse
 
silentwatcher_95 profile image
Ali nazari

Thank you for your positive feedback! ๐Ÿค—

Collapse
 
msamgan profile image
Mohammed Samgan Khan

MJML is an excellent resource for mailing. That is why I put it in default in Framework X. Check it out.

Collapse
 
ronaldohoch profile image
Ronaldo Hoch

I'm using this in my work, it's awesome.

Collapse
 
davitacols profile image
David Ansa

Is it free and integrate google's smtp for personal email use case testing?
I can be interested can it be used with PHP Laravel

Collapse
 
silentwatcher_95 profile image
Ali nazari

I think it can be used for any email services and to integrate MJML into your PHP app, you have to use its API.
happy coding ! ๐Ÿค—

Collapse
 
stupid_evening profile image
Stupid Evening

thanks for sharing.. im glad to reach your posting.

Collapse
 
silentwatcher_95 profile image
Ali nazari

You're welcome! I'm glad you found it helpful.