【问题标题】:MailGun: Add data variables to emailMailGun:将数据变量添加到电子邮件
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 啊完美。谢谢这工作。我会接受答案。您是否知道如何在 Mailgun 电子邮件正文中列出数组值?目前我在正文中执行%recipient.projects%,但显示[u'aqeqw weqw ', u'aqeqw weqw ', u'james title']
  • 不太了解 Mailgun api,但从 doc 看来,您必须将数据格式化为 JSON。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多