Can anyone weigh in on how to create a dynamic email template to use with firebase cloud functions?
Basically, I have created a function that will be invoked on hitting a /sendEmail endpoint. It holds data in req body, I am retrieving that data and sending it via email using nodemailer.
I am getting a path error if I want to use handlebars to create the template. The error is that no such file or directory exists. I tried two different paths, the first one under functions/src/email/template.html and the second one is at the same level as the functions folder.
sendMail Function
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'example@gmail.com',
pass: 'dqsdsqdqsdsq'
}
});
const filePath = path.join(__dirname, 'functions/src/Email/template.html');
const source = fs.readFileSync(filePath, 'utf-8').toString();
const template = handlebars.compile(source);
const replacements = {
schoolName: schoolName,
className: className,
date: new Date(),
responsibiltyId: responsibiltyId,
memorialId: memorialId
};
const htmlToSend = template(replacements);
const mailOptions = {
from: 'ADMIN <example@gmail.com>', // Something like: Jane Doe <janedoe@gmail.com>
to: destination,
subject: `Session Codes for Class ${className} | ${schoolName} | ${new Date()}`, // email subject
html: htmlToSend
};
return transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return handleError(res, error);
}
return res.status(200).json({
timestamp: new Date(),
status: 200,
message: `Email has been sent successfuly to ${destination}`,
extra: ''
});
});