簡體   English   中英

如何在 mailgun 電子郵件模板(Node js)中分配變量?

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

我制作了一個谷歌雲函數,在其中我發送了一封電子郵件,其中包含我從另一個地方收到的一些變量。 我正在使用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 文檔是這樣說的:

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-jshighcaffeinated/mailgun-js 都沒有指定如何傳遞標題。

根據Mailgun 模板文檔,您可以使用下面提供的 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標頭提供變量。

您可以通過在鍵周圍使用引號將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")

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

你可以這樣使用

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

使用此形狀的文檔頁面

h:X-Mailgun-變量
像那樣

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