[英]MailGun: Add data variables to email
Im sending emails through Node.js using MailGun. 我使用MailGun通过Node.js发送电子邮件。 I want to add custom data variables that contain data from database send it as part of email body.
我想添加包含来自数据库的数据的自定义数据变量,将其作为电子邮件正文的一部分发送。 Based on my research I need to use
recipient-variables
. 根据我的研究,我需要使用
recipient-variables
。 But the problem is that recipient-variables
require the email
of recipient as the key
of the object like so: 但是问题在于
recipient-variables
需要recipient-variables
的email
作为对象的key
,如下所示:
{
"user1@example.com" : {"unique_id": "ABC123456789"},
"user2@example.com" : {"unique_id": "ZXY987654321"}
}
Now the data I'm getting from my database is this: 现在,我从数据库中获取的数据是:
{ email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}
Whereas MailGun requires this (which seems weird to me): 而MailGun要求这样做(在我看来,这很奇怪):
{'myemail@something.com': { email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}}
How can I set the key of object I'm receiving from the database as the email? 如何设置从数据库接收的对象的密钥作为电子邮件?
FYI my code for MailGun: 仅供参考,我的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);
});
You can just do it like this, with an intermediate object: 您可以使用中间对象这样做:
let obj = {
email: 'myemail@something.com',
projects: [ 'aqeqw weqw ', 'title here' ]
}
let final = {}
final[obj.email] = obj
console.log(final)
/* {
'myemail@something.com':
{
email: 'myemail@something.com',
projects: [ 'aqeqw weqw ', 'title here' ]
}
}
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.