For many web apps, sending emails is an expected feature. Whether it is for a signup confirmation, password recovery, new password updating, or even promotional offer, a properly designed email improves the user engagement and chances of interaction. If you have a Node.js application you want to integrate email features for, you are at the right place.

In this guide, you’ll learn how to:

  • Β Set up Nodemailer in Node.js
  • Send HTML-styled emails using custom templates
  • Use variables (like user name) in templates

Configure Nodemailer with Custom Email Templates in Node.js

Step 1: Install Required Packages

First, let’s install Nodemailer and optionally handlebars for dynamic templates:

npm install nodemailer express nodemailer-express-handlebars

Step 2: Project Structure

Here’s a minimal structure:

πŸ“¦ email-demo/
β”œβ”€β”€ πŸ“ templates/
β”‚ └── welcome.handlebars
β”œβ”€β”€ πŸ“ utils/
β”‚ └── mailer.js
β”œβ”€β”€ app.js

Step 3: Create an HTML Email Template

Create templates/welcome.handlebars:

<!DOCTYPE html><html> <body> <h2>Welcome, {{name}}!</h2> <p>Thank you for signing up. We're excited to have you.</p> <p>Regards, <br />The Team</p> </body></html>

Step 4: Configure Nodemailer with Template Support

In utils/mailer.js:
const nodemailer = require('nodemailer');
const hbs = require('nodemailer-express-handlebars');
const path = require('path');
// Transporter config using Gmail SMTP
const transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: process.env.MAIL_USER, // e.g., yourname@gmail.com pass: process.env.MAIL_PASSWORD, // your Gmail App password },
});
// Template engine config
transporter.use('compile', hbs({ viewEngine: { extname: '.handlebars', partialsDir: path.resolve('./templates'), defaultLayout: false, }, viewPath: path.resolve('./templates'), extName: '.handlebars',
}));
// Send mail function
const sendWelcomeEmail = async (toEmail, name) => { const mailOptions = { from: '"My App" <no-reply@myapp.com>', to: toEmail, subject: 'Welcome to My App!', template: 'welcome', context: { name }, // dynamic data }; try { await transporter.sendMail(mailOptions); console.log(`Email sent to ${toEmail}`); } catch (error) { console.error('Email send error:', error.message); }
};
module.exports = { sendWelcomeEmail };

Step 5: Use in Your App

In app.js:

require('dotenv').config();
const express = require('express');
const { sendWelcomeEmail } = require('./utils/mailer');
const app = express();
app.use(express.json());
app.post('/send-welcome', async (req, res) => { const { email, name } = req.body; await sendWelcomeEmail(email, name); res.send('Email sent!');
});
app.listen(3000, () => console.log('Server running on port 3000'));

Conclusion

Nodemailer makes it easy to send personalized HTML emails in your Node.js apps using custom templates and variables. It’s production-friendly and integrates well with most mail providers. If you’re looking to scale notification systems or improve deliverability, you might want to hire Node.js developers who know how to build and maintain reliable email infrastructure from day one.