I’m trying to build an email system for my web application using Express.js and Mailgun service. I want to send HTML emails with dynamic content using Handlebars templates, but I keep running into issues.
Here’s my current setup:
const mail = require('nodemailer');
const handlebars = require('nodemailer-express-handlebars');
const mailgunTransport = require('nodemailer-mailgun-transport');
const hbsConfig = {
viewEngine: {
extname: '.hbs',
layoutsDir: 'templates/mail/',
defaultLayout: 'main',
partialsDir: 'templates/components/'
},
viewPath: 'templates/mail/',
extName: '.hbs'
};
const credentials = {
auth: {
api_key: 'your-mailgun-key',
domain: 'your-domain.com'
}
};
const transport = mail.createTransport(mailgunTransport(credentials));
transport.use('compile', handlebars(hbsConfig));
transport.sendMail({
from: '[email protected]',
to: '[email protected]',
subject: 'Welcome Message',
template: 'welcome',
context: {
userName: 'John',
userEmail: '[email protected]'
}
}, (err, info) => {
if (err) {
console.error('Send failed:', err);
} else {
console.log('Email sent:', info);
}
});
My template files:
templates/mail/main.hbs
{{>mail/head}}
<body>
{{>mail/nav}}
{{{body}}}
{{>mail/bottom}}
</body>
</html>
templates/mail/welcome.hbs
<h2>Welcome to our platform!</h2>
<p>Hello {{userName}}</p>
<p>Your email: {{userEmail}}</p>
But I get this error message:
Error: Sorry: template parameter is not supported yet. Check back soon!
I need to figure out how to properly send templated emails through Mailgun. The same code works fine with other email providers. Has anyone successfully implemented this setup? What’s the correct approach for using templates with Mailgun transport?