2

Well I have an API which sends an email through Nodemailer. I want the emails to be rendered as HTMLs thus I tried to connect nodemailer-express-handlebars to my api:

const nodemailer = require('nodemailer') const hbs = require('nodemailer-express-handlebars) const sendMail = async (options) => { try { const transporter = nodemailer.createTransport({ host: process.env.EMAIL_HOST, port: process.env.EMAIL_PORT, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, }) transporter.use( 'compile', hbs({ viewEngine: { extName: '.hbs', partialsDir: './views/', layoutsDir: './views/layouts', defaultLayout: '', }, extName: '.hbs', viewPath: 'views', }) ) const mailOptions = { from: '[email protected]', // to: '[email protected]', to: options.email, bcc: '[email protected]', subject: 'Booking confirmation', template: 'emailHtml', } await transporter.sendMail(mailOptions) } catch (err) { return new AppError( 'There was an error while sending the email. Please, try again later.', 500 ) } } 

Here is my files structure for that api (the api itself is in email.js, and the util folder is in the root):

The api itself is in email.js

In the main.hbs there is just standard html5 markup with {{{body}}} in its body and in emailHtml.hbs there is just hello world in h1 tag.

However, it doesn't work (I thought the problem could be in wrong path but I tried lots of combinations). I know there is a way to just specify html: <h1> hello world </h1> using styles and variables but this is not an appropriate solution for me, so a modular one would be very helpful to me. When I remove hbs configuration and put just plain text in mailOptions emails have been delivered, but once I put template it stops working. Dotenv is configured, I just didn't put it here.

Any ideas?

2
  • Have you tried compiling the handlebar template into a string and attaching to email body manually without using the transporter compile options? It may be worth the try to see if that's working. Also, if possible, check your handlebar template for syntax errors. Commented Jan 13, 2021 at 19:22
  • the same again😕 Commented Jan 14, 2021 at 4:20

1 Answer 1

1

if email.js and layouts folder are in same folder than following code will work.

const nodemailer = require('nodemailer') const hbs =require('nodemailer-express-handlebars') const sendMail = async (options) => { try { const transporter = nodemailer.createTransport({ host: process.env.EMAIL_HOST, port: process.env.EMAIL_PORT, auth: { user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS, }, }) transporter.use( 'compile', hbs({ viewEngine: { extName: '.hbs', partialsDir: 'layouts/', layoutsDir: 'layouts/', defaultLayout: 'emailHtml', }, extName: '.hbs', viewPath: 'layouts/', }) ) const mailOptions = { from: '[email protected]', // to: '[email protected]', to: options.email, bcc: '[email protected]', subject: 'Booking confirmation', template: 'emailHtml', } await transporter.sendMail(mailOptions) } catch (err) { return new AppError( 'There was an error while sending the email. Please, try again later.', 500 ) } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.