无法使用SendGrid的API V3进行替换

时间:2017-01-08 18:50:44

标签: sendgrid-api-v3

我使用SendGrids Web API v3成功发送电子邮件,但在使用模板发送电子邮件时无法获取替换信息。电子邮件成功通过,但没有替换发生。这是我的(简化)SendGrid模板:

<html>
<head>
    <title></title>
</head>
<body>
<div>You can reset your password by clicking on this link:&nbsp;{callbackUrl}</div>

<%body%>

</body>
</html>

这是我发送的JSON格式化主体:

{
   "personalizations":[
      {
         "to":[
            {
               "email":"destinationemail@gmail.com"
            }
         ]
      }
   ],
   "sub" : {
        "{callbackUrl}" : "www.mysite.com/changepassword"
   },
   "from":{
      "email":"example@example.com"
   },
   "template_id" :"5f852a2e-996b-4f04-be05-31766d1092d8",
   "subject":"Reset your password",
   "content": 
   [
    {"type": "text/plain", "value": "Thank you"}]
}

使用正确的模板主题生成电子邮件,并将其发送到正确的电子邮件地址,但电子邮件正文包含不带替换的模板:

You can reset your password by clicking on this link: {callbackUrl}

Thank you

我不知道自己做错了什么。我正在使用Postman在尝试追踪问题的同时发送请求。

1 个答案:

答案 0 :(得分:0)

您需要在substitutions下使用密钥personalizations,而不是文档中所说的sub。它的结构也不同于sub

{
  "personalizations" : [ {
    "substitutions" : {
      ":name" : "John"
    },
    "subject" : "Hello from Java",
    "to" : [ {
      "email" : "john@example.com"
    } ]
  } ],
  "from" : {
    "email" : "john@example.com"
  },
  "template_id" : "11111111-1111-1111-1111-111111111111"
}