dynamic_template_data 在使用 curl 请求发送动态模板时不起作用 - Sendgrid API

问题描述 投票:0回答:2

我正在发出 curl 请求以发送带有车把的动态模板,但它无法正常工作。

curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer SG.xxxxxxxx.v-8xxxxxxxxxxxxI' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "[email protected]"}]}],"from": {"email": "[email protected]"},"dynamic_template_data":{"fname":"elliot"}, "subject": "Hello, World!","content": [{"type": "text/html", "value": "Heya!"}], "template_id" : "d-xxxxxxxxxa1f"}}'

在我的动态模板中,我将 fname 用作

{{fname}}
,但是当我发出 curl 请求时它变成空的。

email curl sendgrid sendgrid-api-v3
2个回答
1
投票

dynamic_template_data 应该在personalizations 里面。 例如:

{
  "personalizations": [
    {
      "to": [
        {
          "email": "[email protected]"
        }
      ],
      "dynamic_template_data": {
        "fname": "elliot"
      }
    }
  ],
  "from": {
    "email": "[email protected]"
  },
  "subject": "Hello, World!",
  "content": [
    {
      "type": "text/html",
      "value": "Heya!"
    }
  ],
  "template_id": "d-xxxxxxxxxa1f"
}

0
投票

我也有同样的问题,但根据 sendgrid 的文档,我们必须用主题

{{subject}}
更新我们的模板并在
dynamic_template_data

中传递主题
 const data = {
  personalizations: [
    {
      to: [
        {
          email: invitee_email,
        },
      ],
      dynamic_template_data: {
        subject: "Write your subject here",
        ...
      },
    },
  ],
  from: {
    email: sender_email,
    name: "Some Name",
  },
  template_id: "Unique template ID",
};

© www.soinside.com 2019 - 2024. All rights reserved.