首页 > 解决方案 > Mailgun.js 是否提供发送模板的可能性?

问题描述

因此MailGun提供了通过实现其API的 Node 库发送电子邮件的可能性:

var mailgun = require('mailgun-js')({ apiKey: api_key, domain: DOMAIN });

var filepath = path.join(__dirname, 'sample.jpg');

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',
  cc: 'baz@example.com',
  bcc: 'bar@example.com',
  subject: 'Complex',
  text: 'Testing some Mailgun awesomness!',
  html: "<html>HTML version of the body</html>",
  attachment: filepath
};

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});

它们还提供了设计和创建电子邮件模板的可能性。有没有办法通过他们的 API 发送带有一些自定义变量的模板化电子邮件?就像是:

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',

  template: "withdraw_request_approved", //Instead of 'html'
  vars: { firstName: 'John', lastName: 'Doe' }
};

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});

如果没有,您能否推荐一些其他提供这种功能的邮件服务?(我已经跳过了Mandrill,因为它显然目前已经关闭,没有明确估计它何时会再次可用)

标签: node.jsemailmailgunmailing

解决方案


是的,您可以,以下是您的情况的格式:

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',

  template: "withdraw_request_approved", //Instead of 'html'
  'v:firstName': 'John',
  'v:lastName': 'Doe'
};

推荐阅读