首页 > 解决方案 > Node.js 使用模板发送 Mailgun 邮件失败,错误:无法解码变量

问题描述

我第一次尝试在我的后端服务器中发送邮件。到目前为止,我正在探索 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);
  
  });
}

谁能帮我解决这个问题?

标签: node.jsemailtemplatesvariablesmailgun

解决方案


我通过这篇文章的答案找到了我的问题的答案 -->使用 Python 通过 Mailgun 提交变量时出错


推荐阅读