首页 文章

MailGun:将数据变量添加到电子邮件中

提问于
浏览
0

我正在使用MailGun通过Node.js发送电子邮件 . 我想添加包含数据库数据的自定义数据变量,将其作为电子邮件正文的一部分发送 . 根据我的研究,我需要使用 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):

{'myemail@something.com': { email: 'myemail@something.com', projects: [ 'aqeqw weqw ', 'title here' ]}}

如何将我从数据库接收的对象的密钥设置为电子邮件?

FYI my code for 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);

            });

1 回答

  • 0

    您可以这样做,使用中间对象:

    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' ]
          }
        }
    */
    

相关问题