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

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

我第一次尝试在我的后端服务器中发送邮件。到目前为止,我正在探索 Mailgun,我成功地发送了包含一些可变细节的基本文本邮件。我现在正在尝试使用我在 Mailgun 中制作的模板,其中我还使用了我传递的变量。

邮件发送后,我可以看到邮件在 Mailgun 中处于失败状态,并显示投递消息:Failed to decode variables

我不明白我做错了什么

这些是创建的模板邮件期望获得的变量:

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

邮寄功能

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);
  
  });
}

谁能帮我解决这个问题?

我通过这个post的回答找到了我问题的答案 -->

万一其他人无意中发现了这一点,对于 Javascript 你只需要 stringify 你的变量。例如:

h:X-Mailgun-Variables: JSON.stringify({ 
  clientFirstname: "test_clientFirstname",
});