【发布时间】:2021-05-14 01:23:02
【问题描述】:
我有一个 python 脚本,它从我们的数据库中提取客户交易数据并将其格式化以供 SendGrid 的动态模板使用,但我无法从 SendGrid 文档或任何其他来源中弄清楚如何在他们的电子邮件中插入每个客户的唯一数据.这是脚本中的相关代码(假设所有变量都由脚本预先填充):
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To
message = Mail(
from_email=("no_reply@mywebsite.com", "My Website"),
to_emails=to_emails,
is_multiple=True)
message.template_id = "d-thetemplateidthatiamusing"
try:
sendgrid_client = SendGridAPIClient("SG.thesecretkeythatdoesnotbelongonstack")
sendgrid_client.send(message)
except Exception as e:
print(e)
当我运行它时,它会发送给正确的客户电子邮件,但没有任何自定义数据。我准备好为每个客户发送以下对象,但不知道如何通过电子邮件将其附加到电子邮件中:
{
"name": "Customer Name",
"games": [
{
"game_title": "Test Team vs. Demo Team",
"event_name": "Live Showcase I",
"date": "5/9/2021",
"score": "79-55",
"url": "app.website.com/"
},
{
"game_title": "Test Team vs. Demo Team",
"event_name": "Live Showcase I",
"date": "5/9/2021",
"score": "79-69",
"url": "app.website.com/"
}
]
}
当我将此 JSON 放入 SendGrid 模板测试数据区域时,它与我的设计完美配合。有什么想法吗?
【问题讨论】:
标签: python sendgrid sendgrid-api-v3