通过Python API Wrapp对SendGrid API v3使用替换时请求错误

2024-10-03 15:23:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过sendgridui创建了一个很好的模板,在尝试通过sendgridpythonapi包装器(v5.4.1)发送电子邮件时遇到了一些问题。我有一个模板的模板id,该模板包含以下(截断)文本:

Hello, {{name}}!

Click the following link to verify your account: {{verification_url}}.

但是,当遵循example in the documentation时,每当我包含个性化设置时,都会收到一个400错误的请求错误。我包括以下个性化设置:

mail.personalizations[0].add_substitution(Substitution("{{name}}", "Example User"))

此外,mail.get()返回以下内容:

^{pr2}$

有什么方法可以调试出正在发生的事情吗?不幸的是,400个错误的请求并没有那么有帮助。。。在

看起来这些功能实际上还不受支持:https://github.com/sendgrid/sendgrid-python/issues/591


Tags: thename文本模板idhello错误link
2条回答

几周前我也遇到过这个问题。替换的格式已更改为使用动态模板数据。在

而不是:

    mail.personalizations[0].add_substitution(Substitution("{{name}}", "Example User"))

使用:

^{pr2}$

get()返回替换的JSON就绪版本。另外,请确保您使用的是至少5.6.0版的SendGridAPI。此功能刚刚添加,以下是指向github提交的链接:https://github.com/sendgrid/sendgrid-python/commit/4be8d580ec15f1f10180a562aeace8478f76597e

API V3最近有一个变化,这对我有用

import os import sendgrid from sendgrid.helpers.mail import Mail, Email, Personalization

    sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY'))
    mail = Mail()
    mail.from_email = Email('customerservice@jafutexpress.com.ng')
    mail.subject = "You are welcome!"
    mail.template_id = 'template_id'
    p = Personalization()
    p.add_to(Email('test@example.com'))
    p.dynamic_template_data = {
    'name': 'Bob',
    }
    mail.add_personalization(p)
    response = sg.client.mail.send.post(request_body=mail.get())
    print(response.status_code)
    print(response.headers)
    print(response.body)

相关问题 更多 >