ParticleDuality
ParticleDuality

Reputation: 149

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.

Upvotes: 4

Views: 3649

Answers (4)

Mohammed Al-Reai
Mohammed Al-Reai

Reputation: 2786

you can use like this

 const data = {
  from: 'Excited User <[email protected]>',
  to,
  subject,
  template,
  'v:code': code,
  'v:username': email
}

the document page it was used this shape

h:X-Mailgun-Variables
like that

look to decument site site

  const data = {
          from: 'Excited User <[email protected]>',
          to,
          subject,
          template,
h:X-Mailgun-Variables: `{"title":${title}, "body": ${body}}'
          
        }

Upvotes: 1

kuttumiah
kuttumiah

Reputation: 555

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.

Upvotes: 11

Tom Giagtzoglou
Tom Giagtzoglou

Reputation: 21

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

Upvotes: 2

Engineer S. Saad
Engineer S. Saad

Reputation: 384

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

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 
}

Upvotes: 1

Related Questions