如何在 mailgun 电子邮件模板(Node js)中分配变量?

How to assign variables in a mailgun email template (Node js)?

我制作了一个 google 云函数,我在其中发送了一封电子邮件,其中包含我从其他地方收到的一些变量。 我正在使用 mailgun.js 并尝试使用我已经在 mailgun 中创建的模板发送电子邮件。 问题是我找不到替换模板中占位符变量的方法。

这是代码:

mg.messages.create('domain', {
    from: 'email',
    to: [email],
    subject: 'subject',
    template: 'template',
    // How to replace the template variables???
  })
  .then(res => console.log('Resolved >>>>> ', res))
  .catch(err => console.log('MAILGUN ERROR >>>> ', err))

mailgun docs 是这样说的:

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"}' // Notice this
};

据我所知,不能将“h:X-Mailgun-Variables”写成任何 object.

中的键

有人知道我需要把它放在哪里或怎么放吗?

我认为它应该作为 header 发送,但 mailgun/mailgun-js nor highlycaffeinated/mailgun-js 均未指定如何传递 headers。

我在 NodeJs 中做过同样的事情,但使用的是 Nodemailer 所以首先我使用 EJS 渲染文件并将变量发送到文件,然后将相同的文件发送给用户

所以它帮助我在我的文件中分配了不同的属性,因为我喜欢这里是代码

function generateToken_And_SendMail(user) 
{
   token = jwt.sign(user,process.env.privateKey)
  ejs.renderFile(__dirname + '/verification_email.ejs',{verify_token : `${process.env.localhost_address}/verifyToken?Authorization=Bearer%20${token}`
                                                        ,username : user.Fullname},(error,file)=>
  {
    if(error)
    console.log(error)
    else
    sendmail_Config(file,user.userEmail,'Email Verification')
  })
   return token 
}

您可以通过在键周围使用引号将 h:X-Mailgun-Variables 设置为键。

但是,您需要使用括号表示法访问对象内的值。

例如

const foo = {
  "ba ar": "foobar",
  "test" : "test"
}

console.log(foo["ba ar"], foo.test)
// #> foobar test


//doesn't work
console.log(foo."ba ar")

根据 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.

你可以这样使用

 const data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to,
  subject,
  template,
  'v:code': code,
  'v:username': email
}

使用这种形状的文档页面

h:X-Mailgun-变量
像那样

查看文档站点 site

  const data = {
          from: 'Excited User <me@samples.mailgun.org>',
          to,
          subject,
          template,
h:X-Mailgun-Variables: `{"title":${title}, "body": ${body}}'
          
        }