Sendgrid: Send email with template
email, sendgrid
Solution
I've looked into the source code and there is a way to pass in dynamic variables.
welcome.html
<p>Welcome %email%</p>
email.js
var file = "welcome.html"
var stringTemplate = fs.readFileSync("./" + file, "utf8");
//create new Emaik object
var email = new sendgrid.Email();
email.addTo(to);
email.setFrom(from);
email.setSubject(subject);
email.setHtml(stringTemplate); //pass in the string template we read from disk
email.addSubstitution("%email%", to); //sub. variables
sendgrid.send(email, function(err, res){
//handle callbacks here
});
Problem
I am trying to send emails with SendGrid and am trying to have multiple templates for different cases. My function looks like this: ``` var file = "welcome.html" sendgrid.send({ to: to, from: from, subject: subject, data: { //template vars go here email: to, confirmLink: confirmLink }, template: "./" + file }, function(err, json) { if (err) { return console.error(err); } console.log(json); }); ``` But when I am sending the email I get ``` [Error: Missing email body] ``` Would there be any way to attach html templates, since I don't want to have hard-coded strings with html content? Edit Reading and converting the file into a string works, but I am unsure how to pass in dynamic variables into the template.. Any suggestions?