Mailgun.js 是否提供发送模板的可能性?
Does Mailgun.js offer the possibility to send a template?
所以MailGun offers the possibility to send email via their Node library that implements their API:
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);
});
而且他们还提供了设计和创造的可能性Email Templates。有什么方法可以通过 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,因为它目前显然已关闭,没有明确估计何时可以再次使用)
可以,您的情况格式如下:
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'
};
根据 Mailgun Template Documentation,您可以使用下面提供的 2 个选项中的任何一个来传递模板数据,
选项 1
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
h:X-Mailgun-Variables: '{"title": "API Documentation", "body": "Sending messages with templates"}'
};
在这个例子中h:X-Mailgun-Variables
这是我实现像这样更新我的对象的棘手部分。
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
'h:X-Mailgun-Variables': JSON.stringify({
title: "API Documentation",
body: "Sending messages with templates"
})
};
选项 2
虽然这已经在 中进行了解释,但为了完整起见我还是添加了这个。
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
'v:title': 'API Documentation',
'v:body': 'Sending messages with templates'
};
最后,根据他们的文档
The second way (Option 2 in our case) is not recomended as it’s limited to simple key value
data. If you have arrays, dictionaries in values or complex json data
you have to supply variables via X-Mailgun-Variables
header.
所以MailGun offers the possibility to send email via their Node library that implements their API:
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);
});
而且他们还提供了设计和创造的可能性Email Templates。有什么方法可以通过 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,因为它目前显然已关闭,没有明确估计何时可以再次使用)
可以,您的情况格式如下:
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'
};
根据 Mailgun Template Documentation,您可以使用下面提供的 2 个选项中的任何一个来传递模板数据,
选项 1
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
h:X-Mailgun-Variables: '{"title": "API Documentation", "body": "Sending messages with templates"}'
};
在这个例子中h:X-Mailgun-Variables
这是我实现像这样更新我的对象的棘手部分。
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
'h:X-Mailgun-Variables': JSON.stringify({
title: "API Documentation",
body: "Sending messages with templates"
})
};
选项 2
虽然这已经在
var data = {
from: 'Excited User <me@samples.mailgun.org>',
to: 'alice@example.com',
subject: 'Hello',
template: 'template.test',
'v:title': 'API Documentation',
'v:body': 'Sending messages with templates'
};
最后,根据他们的文档
The second way (Option 2 in our case) is not recomended as it’s limited to simple key value data. If you have arrays, dictionaries in values or complex json data you have to supply variables via
X-Mailgun-Variables
header.