【问题标题】:Can't get substitutions working with SendGrid's API V3无法使用 SendGrid 的 API V3 进行替换
【发布时间】:2017-01-08 18:50:43
【问题描述】:

我正在使用 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 发送请求,同时尝试追踪问题。

【问题讨论】:

    标签: sendgrid-api-v3


    【解决方案1】:

    您需要在personalizations 下使用密钥substitutions,而不是文档中所说的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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多