4

So MailGun offers the possibility to send email via their Node library that implements their API:

var mailgun = require('mailgun-js')({ apiKey: api_key, domain: DOMAIN });

var filepath = path.join(__dirname, 'sample.jpg');

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',
  cc: 'baz@example.com',
  bcc: 'bar@example.com',
  subject: 'Complex',
  text: 'Testing some Mailgun awesomness!',
  html: "<html>HTML version of the body</html>",
  attachment: filepath
};

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});

And they also offer the possibility to design and create Email Templates. Is there any way to send templated emails with some custom variables via their API? Something like:

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',

  template: "withdraw_request_approved", //Instead of 'html'
  vars: { firstName: 'John', lastName: 'Doe' }
};

mailgun.messages().send(data, function (error, body) {
  console.log(body);
});

If not, could you suggest some other mailing service that offer this kind of functionality? (I've skipped Mandrill since it's apparently currently down, with no clear estimate to when it'll become available again)

iuliu.net
  • 6,666
  • 6
  • 46
  • 69

2 Answers2

14

Yes, you can, following would be the format in your case:

var data = {
  from: 'Excited User <me@samples.mailgun.org>',
  to: 'foo@example.com, baz@example.com, bar@example.com',

  template: "withdraw_request_approved", //Instead of 'html'
  'v:firstName': 'John',
  'v:lastName': 'Doe'
};
adnan kamili
  • 8,967
  • 7
  • 65
  • 125
3

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

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

Though this is already explained in a previous answer, I am addition this for completeness.

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. If you have arrays, dictionaries in values or complex json data you have to supply variables via X-Mailgun-Variables header.

kuttumiah
  • 525
  • 1
  • 11
  • 19