Search code examples
node.jsemailtemplatesvariablesmailgun

Node.js send Mailgun mail with template fails , error : Failed to decode variables


I am trying to send mails in my back-end server for the first time. I'm exploring Mailgun so far I succeeded to send basic text mails with some variable details in it. I'm now trying to use a template that I made in Mailgun where I also used variables that I got pass along.

When the mail gets send I can see that is has a failed status in Mailgun with a delivery message : Failed to decode variables

I don't see what I could be doing wrong

these are the variables the created template mail expects to get:

{
    "clientFirstname": "test_clientFirstname",
    "serviceName": "test_serviceName",
    "date": "test_date",
    "start": "test_start",
    "end": "test_end"
}

Mailing function

function sendMail(emailStatus, client, service , args ){
  const DOMAIN = 'dummy_domain';
  const api_key ='dummy_apikey';
  const mg = mailgun({apiKey: api_key, domain: DOMAIN});

  const date  =  moment(args.start).format('dddd, MMMM Do YYYY  ');
  const start =  moment(args.start).format('HH:mm');
  const end   =  moment(args.end).format('HH:mm');

  var data = {};

  switch (emailStatus) {
    case "create_appointment":
      data = {
        from: 'Afsprakenbeheer <dummy_mail>',
        to: 'dummy_mail',
        subject: 'Confirmation appointment',
        template: "create-appointment",
        h:X-Mailgun-Variables: '{"clientFirstname": "test_clientFirstname", "serviceName": 
        "test_serviceName","date": "test_date","start": "test_start",  "end": "test_end"}'
       };
      break;

    case "update_appointment":
       data = {};
      break;

    case "cancel_appointment":
       data = {};
      break;

  
    default:
      break;
  }


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

Could anyone help me with this problem?


Solution

  • I found the answer to my question through the answer of this post --> Error when submitting variables through Mailgun with Python