string - SendGrid 使用 template_id 进行字符串替换

标签 string templates str-replace sendgrid sendgrid-api-v3

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

我创建了一个模板并对其进行了引用。在此模板中,我放置了一些要替换的变量。邮件将成功发送,但不会进行字符串替换。

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

{ "template_id": "d-1074861686174fbfac02e25381e02e32", "personalizations": [ { "to": [{"email": "[email protected]"}], "sub": { "%USERNAME%": ["Hans"], "%DATE%": ["25.12.1988"], "%DAYS%": ["58"], }, }, ], "from": { "email": "[email protected]", "name": "Kundenservice" }, "reply_to": { "email": "[email protected]", "name": "Kundenservice" } }

最佳答案

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

{{ email }}

问题是现在替换键更改为dynamic_template_data

 {
   "from":{
      "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddb8a5bcb0adb1b89df3aeb8b3b9baafb4b9f3b3b8a9" rel="noreferrer noopener nofollow">[email protected]</a>"
   },
   "personalizations":[
      {
         "to":[
            {
               "email":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e98c91888499858ca99a8c878d8e9b808dc7878c9d" rel="noreferrer noopener nofollow">[email protected]</a>"
            }
         ],
         "dynamic_template_data":{
            "name":"Sample Name",
            "city":"Place",
            "state":"CO"
          }
      }
   ],
   "template_id":"[template_id]"
}

尝试这个示例,让我知道它是否适合您:)

还有官方文档,非常清楚地解释了整个发送过程 How to send an email with Dynamic Transactional Templates

关于string - SendGrid 使用 template_id 进行字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52237105/

相关文章:

Python 字符串或 if 语句问题

java - 对于 Java 中的 "[,,.\\s]",正则表达式拆分字符串效果不佳

php - 为什么我的 Blade 模板内容显示两次?

c++ - 通过头文件函数将二叉搜索树中遍历的数据加载到Vector中

python - 在Python中删除 '\xad'的最佳方法?

batch-file - 需要从 1 行很长的文本文件中替换 13 个空格

javascript - 外部更换功能不起作用

r - 在 R 中解码字符串向量

java - 将用户输入转换为乱七八糟的布偶谈话

c++ - 如何根据参数化类型的特定成员的类型对模板类函数进行不同的实现