node.js - 使用 sendgrid Node 模块,如何发送 SendGrid 模板引擎电子邮件?

标签 node.js sendgrid

我已经通过 SendGrid 的模板引擎创建了一个“感谢订阅”电子邮件模板。

现在,当有人订阅我的网站时,我想向他发送该模板。我可以使用 sendgrid-nodejs 来做到这一点吗?包?

我在文档中没有看到任何关于此的内容。

最佳答案

是的,这真的很简单,您只需将其添加为过滤器即可。它应该是这样的:

var cardEmail = new sendgrid.Email({
  to: "theuser@somedomain.com",
  from: "bignews@yourdomain.com",
  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 的博文。 .

关于node.js - 使用 sendgrid Node 模块,如何发送 SendGrid 模板引擎电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29031169/

相关文章:

Eclipse Node.js 项目没有在断点处停止

javascript - 在 URL 中构造查询参数时,删除 qs.stringify 中具有空值或 null 值的键

node.js - 设置 Heroku Scheduler 作业以向所有用户发送电子邮件 (Meteor/MongoDB)

node.js - 如何确保每个用户只打开一个 socket.io 连接?

email - 从 root@servername 更改外发邮件地址 - rackspace sendgrid postfix

c# - 如示例所示,将 SendGrid 包与 C# "Web"一起使用是未定义的

c# - 如何仅为设置的特定 StatusCodes 设置 Polly Retry

javascript - 将客户电子邮件传递给 strip 结帐

azure - 在 Azure 中使用 SendGrid 从 @my-domain.com 发送电子邮件

node.js - 如何在 log4js smtp 附加程序中使用非 SMTP 传输?