我已經通過SendGrid的模板引擎創建了「感謝訂閱」電子郵件模板。使用sendgrid節點模塊,如何發送SendGrid模板引擎電子郵件?
現在,當有人訂閱我的網站時,我想發送那個人那個模板。我可以使用sendgrid-nodejs包嗎?
我在文檔中沒有看到任何與此有關的內容。
我已經通過SendGrid的模板引擎創建了「感謝訂閱」電子郵件模板。使用sendgrid節點模塊,如何發送SendGrid模板引擎電子郵件?
現在,當有人訂閱我的網站時,我想發送那個人那個模板。我可以使用sendgrid-nodejs包嗎?
我在文檔中沒有看到任何與此有關的內容。
是的,這真的很容易,你只需要在添加它作爲一個過濾器。以下是它的外觀:
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!');
}
});
我希望能幫到你。如果您需要更多洞察力,請查看我寫的有關using Template Engine with sendgrid-nodejs的博文。
看起來您需要使用SMTPAPI的應用程序(過濾器)方法發送模板。我不相信它還沒有被web API支持。下面是一些文檔:
https://sendgrid.com/docs/API_Reference/SMTP_API/apps.html#templates
調用模板只能在X-smtpapi參數來完成,但是這樣的說法可以在[webapi調用](https://sendgrid.com/docs/API_Reference/Web_API/mail.html#-send) – jacobmovingfwd 2015-03-16 16:59:37
,使其與版本4.xx的使用本作品:
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');
什麼是替代?像Mandrill一樣的標籤? :) – jdnichollsc 2016-04-27 13:42:40
這不再適用於最新的SDK :( – 2017-02-01 13:51:47