Advertisement
Intermediate Node.js, Email & Notifications

What is email template rendering, and how might you use a templating engine to generate dynamic HTML emails in Node.js?

Short answer

Rather than building HTML email bodies via manual string concatenation, a templating engine (like Handlebars, EJS, or a dedicated email-templating library like MJML) lets you define reusable email layouts with placeholders for dynamic content (a user's name, an order summary)…

Save Node.js mock interview
Advertisement
AModel answer
Rather than building HTML email bodies via manual string concatenation, a templating engine (like Handlebars, EJS, or a dedicated email-templating library like MJML) lets you define reusable email layouts with placeholders for dynamic content (a user's name, an order summary), rendered into final HTML at send time -- MJML specifically compiles a simplified markup into HTML/CSS that's been tested to render consistently across the notoriously inconsistent rendering engines used by different email clients.
const mjml2html = require('mjml');
const Handlebars = require('handlebars');

const template = Handlebars.compile(mjmlSource);
const filledMjml = template({ userName: user.name, orderTotal: order.total });
const { html } = mjml2html(filledMjml);

await transporter.sendMail({ to: user.email, subject: 'Order Confirmation', html });
Real-world example
A team building order-confirmation emails uses MJML for the layout (guaranteeing consistent rendering across Outlook, Gmail, and Apple Mail) combined with Handlebars for injecting the specific order details, rather than hand-writing fragile, client-inconsistent raw HTML.
Likely follow-ups
  • Why is writing HTML email templates significantly more constrained than writing regular web HTML/CSS?
  • How would you preview a rendered email template across different email clients before sending it to real users?
Related topics