简体   繁体   English

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

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

I've made a google cloud function in which I send an email with some variables I receive from another place.我制作了一个谷歌云函数,在其中我发送了一封电子邮件,其中包含我从另一个地方收到的一些变量。 I'm using mailgun.js and I'm trying to send the email with a template I've already created in mailgun.我正在使用mailgun.js并且我正在尝试使用我已经在 mailgun 中创建的模板发送电子邮件。 The issue is that I can't find a way to replace the placeholder variables in my template.问题是我找不到替换模板中占位符变量的方法。

This is the code:这是代码:

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))

The mailgun docs says this: mailgun 文档是这样说的:

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
};

As far as I know one cannot write " h:X-Mailgun-Variables " as a key in any object.据我所知,不能将“ h:X-Mailgun-Variables ”写为任何对象中的键。

Does anybody know where or how do I need to put it?有人知道我需要把它放在哪里或如何放吗?

I thought that it should be sent as a header but neither mailgun/mailgun-js nor highlycaffeinated/mailgun-js specifies how to pass headers.我认为它应该作为标题发送,但mailgun/mailgun-jshighcaffeinated/mailgun-js 都没有指定如何传递标题。

According to Mailgun Template Documentation you can pass template data using any of the 2 options provided below,根据Mailgun 模板文档,您可以使用下面提供的 2 个选项中的任何一个传递模板数据,

Option 1选项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"}'
};

In this example h:X-Mailgun-Variables this is the tricky bit which I achieved updating my object like this.在这个例子中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"
  })
};

Option 2选项 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'
};

Finally, according to their documentation最后,根据他们的文档

The second way ( Option 2 in our case) is not recomended as it's limited to simple key value data.不推荐第二种方式(在我们的例子中是选项 2 ),因为它仅限于简单的键值数据。 If you have arrays, dictionaries in values or complex json data you have to supply variables via X-Mailgun-Variables header.如果您有数组、值中的字典或复杂的 json 数据,您必须通过X-Mailgun-Variables标头提供变量。

You can set h:X-Mailgun-Variables as a key by using quotes around the key.您可以通过在键周围使用引号将h:X-Mailgun-Variables为键。

You need to access the value within the object using bracket notation however.但是,您需要使用括号表示法访问对象内的值。

For example例如

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

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


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

i have done same thing in NodeJs but using Nodemailer So first i have render the file using EJS and by sending the variables to the file and then send the same file to user我在 NodeJs 中做了同样的事情,但使用 Nodemailer 所以首先我使用 EJS 渲染文件,并将变量发送到文件,然后将相同的文件发送给用户

So it helped me to assigned different attribute in my file as i like here is the code所以它帮助我在我的文件中分配了不同的属性,因为我喜欢这里是代码

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 
}

you can use like this你可以这样使用

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

the document page it was used this shape使用此形状的文档页面

h:X-Mailgun-Variables h:X-Mailgun-变量
like that像那样

look to decument site site看看 decument 网站

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM