Search code examples
stringtemplatesstr-replacesendgridsendgrid-api-v3

SendGrid using string replacement with template_id


I try to send a single Email via SendGrid API v3 with curl call.

I created a template and reference on it. In this template i placed some vars to be replace. Mails will be send successfully, but without it's string replacement.

There are the datastructure I sent. What do I wrong?

{ "template_id": "d-1074861686174fbfac02e25381e02e32", "personalizations": [ { "to": [{"email": "test@test.de"}], "sub": { "%USERNAME%": ["Hans"], "%DATE%": ["25.12.1988"], "%DAYS%": ["58"], }, }, ], "from": { "email": "service@test.de", "name": "Kundenservice" }, "reply_to": { "email": "support@test.de", "name": "Kundenservice" } }


Solution

  • The variables that you want to replace in your template should be in curly braces

    {{ email }}

    The thing is that now the substitution key changed to dynamic_template_data

     {
       "from":{
          "email":"example@.sendgrid.net"
       },
       "personalizations":[
          {
             "to":[
                {
                   "email":"example@sendgrid.net"
                }
             ],
             "dynamic_template_data":{
                "name":"Sample Name",
                "city":"Place",
                "state":"CO"
              }
          }
       ],
       "template_id":"[template_id]"
    }
    

    Try this example and let me know if it works for you :)

    There is also official documentation where it is explained very clear the entire sending process How to send an email with Dynamic Transactional Templates