Reputation: 17583
I have created a "Thanks for subscribing" email template via SendGrid's Template Engine.
Now, when someone subscribes to my site, I want to send that person that template. Can I do this using the sendgrid-nodejs package?
I don't see anything regarding this in the docs.
Upvotes: 2
Views: 4700
Reputation: 1511
Yep, it's really easy, you just need to add it in as a filter. Here's how it should look:
var cardEmail = new sendgrid.Email({
to: "[email protected]",
from: "[email protected]",
subject: process.env.SUBJECT,
html: '<h2>Thanks for requesting a business card!</h2>', // This fills out the <%body%> tag inside your SendGrid template
});
// Tell SendGrid which template to use, and what to substitute. You can use as many substitutions as you want.
cardEmail.setFilters({"templates": {"settings": {"enabled": 1, "template_id": "325ae5e7-69dd-4b95-b003-b0109f750cfa"}}}); // Just replace this with the ID of the template you want to use
cardEmail.addSubstitution('-greeting-', "Happy Friday!"); // You don't need to have a substitution, but if you want it, here's how you do that :)
// Everything is set up, let's send the email!
sendgrid.send(cardEmail, function(err, json){
if (err) {
console.log(err);
} else {
console.log('Email sent!');
}
});
I hope that helps you out. If you need more insight into it, please check out the blog post I wrote about using Template Engine with sendgrid-nodejs.
Upvotes: 14
Reputation: 5588
To make it work with version 4.x.x use this:
var helper = require('sendgrid').mail;
var from_email = new helper.Email('[email protected]');
var to_email = new helper.Email('[email protected]');
var subject = 'I\'m replacing the subject tag';
var content = new helper.Content('text/html', 'I\'m replacing the <strong>body tag</strong>');
var mail = new helper.Mail(from_email, subject, to_email, content);
mail.personalizations[0].addSubstitution(new helper.Substitution('-name-', 'Example User'));
mail.personalizations[0].addSubstitution(new helper.Substitution('-city-', 'Denver'));
mail.setTemplateId('13b8f94f-bcae-4ec6-b752-70d6cb59f932');
Upvotes: 4
Reputation: 17583
It looks like you need to use the SMTPAPI's app(filters) methods for sending of templates. I don't believe it's yet supported by the web API. Here's some docs:
https://sendgrid.com/docs/API_Reference/SMTP_API/apps.html#templates
Upvotes: 0