Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

var data = {
  from: 'Excited User <[email protected]>',
  to: '[email protected]',
  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.

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.

like image 891
ParticleDuality Avatar asked Oct 23 '25 21:10

ParticleDuality


2 Answers

According to Mailgun Template Documentation you can pass template data using any of the 2 options provided below,

Option 1

var data = {
  from: 'Excited User <[email protected]>',
  to: '[email protected]',
  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.

var data = {
  from: 'Excited User <[email protected]>',
  to: '[email protected]',
  subject: 'Hello',
  template: 'template.test',
  'h:X-Mailgun-Variables': JSON.stringify({
    title: "API Documentation",
    body: "Sending messages with templates"
  })
};

Option 2

var data = {
  from: 'Excited User <[email protected]>',
  to: '[email protected]',
  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. If you have arrays, dictionaries in values or complex json data you have to supply variables via X-Mailgun-Variables header.

like image 139
kuttumiah Avatar answered Oct 26 '25 13:10

kuttumiah


You can set h:X-Mailgun-Variables as a key by using quotes around the key.

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

like image 30
Tom Giagtzoglou Avatar answered Oct 26 '25 12:10

Tom Giagtzoglou