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.
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: ''
});
});
Replace this line of code
const filePath = path.join(__dirname, 'functions/src/Email/template.html');
with
const filePath = path.join('', 'src/Email/template.html');
When you deploy your functions, everything in the functions folder is packaged up and sent to Cloud Functions. Nothing outside of that folder is sent, which means that your template.html folder is not available at runtime. If you want to contents to be readable at runtime by your deployed code, it should live in the functions folder before you run the deploy command.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.