Legendary Emails in Node js with mjml 📩
Ali nazari

Ali nazari @silentwatcher_95

About: I'm a dedicated software developer committed for refining my skills and efforting for crafting impactful solutions for the betterment of our community. I actively contribute to open source projects

Location:
Earth 🌍
Joined:
Jul 30, 2022

Legendary Emails in Node js with mjml 📩

Publish Date: Jun 30 '24
82 16

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. 🤗

Comments 16 total

  • Ali nazari
    Ali nazariJun 30, 2024

    by the way the MJML stands for *mailjet markup language * 🚶🏻‍♂️

  • David Ansa
    David AnsaJul 1, 2024

    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

    • Ali nazari
      Ali nazariJul 1, 2024

      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 ! 🤗

  • Justin Young
    Justin YoungJul 1, 2024

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

  • George Cosmin Taschina
    George Cosmin TaschinaJul 4, 2024

    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!

  • Ronaldo Hoch
    Ronaldo HochJul 5, 2024

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

  • Mohammed Samgan Khan
    Mohammed Samgan KhanJul 6, 2024

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

  • Devendra Singh
    Devendra SinghJul 6, 2024

    We can achieve similar context using HTML in email. Is there any use case where only MJML succeeds and MJML fails.

    • Ali nazari
      Ali nazariJul 6, 2024

      One of the things that makes mjml stand out is that you can create responsive templates with it, which can be tedious to do in normal html. 😣
      In addition, mjml avoids the large amount of inline styles that are written in html

  • Neurabot
    NeurabotJul 9, 2024

    Impressive. Innovant.

    • Ali nazari
      Ali nazariJul 9, 2024

      Thank you for your positive feedback! 🤗

  • Stupid Evening
    Stupid EveningSep 9, 2024

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

    • Ali nazari
      Ali nazariSep 19, 2024

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

  • Alexander Swensen
    Alexander SwensenSep 17, 2024

    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)

    • Ali nazari
      Ali nazariSep 19, 2024

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

  • Ajmal Leonard
    Ajmal LeonardOct 3, 2024

    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 🦾

Add comment