I used 'email-templates-v2' for my project. First compile your template file like this:
var EmailTemplate = require('email-templates-v2').EmailTemplate; var templateDir = constants.email_templates_path + 'user-line-won'; var userWonTemplate = new EmailTemplate( templateDir );
Now, for passing context data to your template, create a context object. For example:
var templateContext = {}; templateContext.user = req.session.user; templateContext.campaign = req.campaign; templateContext.project = req.project;
Now, render the template and send the mail in the callback function. You get the compiled template in the success callback:
userWonTemplate.render(templateContext, function (err, results) { if( err ) { console.log( err ); res.render('error', {'title': 'Error', 'error': err, 'session': req.session}); } else { var nodemailer = require("nodemailer"); var mailTransport = nodemailer.createTransport(); var mailOptions = { from: fromEmail, to: toEmail, subject: subjectEmail, text: "A user subject line has won.\n", html: results.html }; mailTransport.sendMail(mailOptions, function(error, response){ if(error){ console.log(error); }else{ console.log("Message sent: " + response.message); } }); next(); } });
I have used nodemailer, but passing the compiled html to sendgrid should work similarly.