Can't get substitutions working with SendGrid's API V3

386 views Asked by At

I am successfully sending emails using SendGrids Web API v3 but can't get the subsitutions to work when sending emails using templates. The emails come through successfully but no substitutions take place. Here is my (simplified) SendGrid template:

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

<%body%>

</body>
</html>

And here is the JSON formatted body I'm sending:

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

The email is generated with the correct template, subject, and is sent to the correct email address however the email body contains the template without substitutions:

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

Thank you

I'm at a loss as to what I'm doing wrong. I'm using currently using Postman to send the request while attempting to track down the issue.

1

There are 1 answers

0
Pavel Vlasov On

You need to use key substitutions under personalizations, not sub as it's said in the docs. Also it has different structure than sub.

{
  "personalizations" : [ {
    "substitutions" : {
      ":name" : "John"
    },
    "subject" : "Hello from Java",
    "to" : [ {
      "email" : "[email protected]"
    } ]
  } ],
  "from" : {
    "email" : "[email protected]"
  },
  "template_id" : "11111111-1111-1111-1111-111111111111"
}