【问题标题】:How to assign variables in a mailgun email template (Node js)?如何在 mailgun 电子邮件模板(Node js)中分配变量?
【发布时间】:2020-06-03 21:39:52
【问题描述】:

我制作了一个谷歌云功能,我在其中发送一封电子邮件,其中包含我从另一个地方收到的一些变量。 我正在使用 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”写为任何对象的键。

有人知道我需要把它放在哪里或如何放置吗?

我认为它应该作为标头发送,但 mailgun/mailgun-jshighlycaffeinated/mailgun-js 都没有指定如何传递标头。

【问题讨论】:

  • 您可以发布您的模板内容吗?您是否存储了您的模板?

标签: javascript node.js mailgun


【解决方案1】:

根据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'
};

最后,根据他们的文档

不推荐第二种方式(在我们的例子中是选项2),因为它仅限于简单的键值 数据。如果您有数组、值中的字典或复杂的 json 数据 您必须通过 X-Mailgun-Variables 标头提供变量。

【讨论】:

    【解决方案2】:

    您可以通过在键周围使用引号将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")
    
    

    【讨论】:

    • 双重检查您的情况,它适用于"h:X-Mailgun-Variables"
    【解决方案3】:

    我在 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 
    }
    

    【讨论】:

      【解决方案4】:

      你可以这样使用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        相关资源
        最近更新 更多