SendGrid 使用字符串替换 template_id

SendGrid using string replacement with template_id

我尝试通过带有 curl 调用的 SendGrid API v3 发送一封电子邮件。

我创建了一个模板并参考了它。在这个模板中,我放置了一些要替换的变量。邮件将成功发送,但没有字符串替换。

有我发送的数据结构。我做错了什么?

{ "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" } }

您要在模板中替换的变量应该在花括号中

{{ email }}

问题是现在替换键更改为 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]"
}

试试这个例子,让我知道它是否适合你:)

还有官方文档,把整个发送过程解释的很清楚 How to send an email with Dynamic Transactional Templates