【发布时间】:2017-12-01 12:40:01
【问题描述】:
我使用 MailGun 通过 Node.js 发送电子邮件。我想添加包含来自数据库的数据的自定义数据变量,将其作为电子邮件正文的一部分发送。根据我的研究,我需要使用recipient-variables。但问题是recipient-variables 要求收件人的email 作为对象的key,如下所示:
{
"user1@example.com" : {"unique_id": "ABC123456789"},
"user2@example.com" : {"unique_id": "ZXY987654321"}
}
现在我从数据库中获取的数据是这样的:
{ email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}
而 MailGun 需要这个(我觉得这很奇怪):
{'myemail@something.com': { email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}}
如何将我从数据库接收到的对象的键设置为电子邮件?
仅供参考我的 MailGun 代码:
let data = {
from: 'My App <donotreply@myapp.com>',
to: data.email,
subject: 'Reminder Email',
'recipient-variables': {data},
html: '<html style="color: red; font-size: 20px; background-color: aqua">Inline image here:<p style="">Your project list and your name is %recipient.projects%</p></html>',
};
mailgun.messages().send(data, function(error, body) {
console.log(body);
});
【问题讨论】:
-
您可以执行类似
var a = { email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}; var b = {[a.email] : a}的操作,然后在b中您将拥有{'myemail@something.com': { email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}} -
@codtex 完美。谢谢!!!
标签: javascript node.js mailgun