400个错误的错误请求动态模板,其中的替代不能在Sendgrid for Python中运行

时间:2020-06-16 08:26:34

标签: python sendgrid sendgrid-api-v3 sendgrid-templates sendgrid-api-v2

问题

我正在尝试使用动态模板发送多封电子邮件,并使用Mail Helper类将每封电子邮件具有自己的唯一替代项。如果不包括替代项,则代码可以正常工作,并且邮件会被传递,但是一旦包含替代项,我就会收到“ HTTP错误400:错误的请求”。 我正在使用虚拟环境,并在我的Flask应用中运行它。 我已经使用“ pip install sendgrid”安装了sendgrid,并将其更新为最新版本。

代码

to_emails = [
        To(
            email='person1@gmail.com',
            name='person1',
            substitutions={
                '-unsubscribeLink-':'https://www.example1.com'
            }
            ),
        To(
            email='person2@gmail.com',
            name='person2',
            substitutions={
                '-unsubscribeLink-':'https://www.example2.com'
            }
            ),
    ]

    message = Mail(
                from_email=From('support@website.com','Website'),
                to_emails=to_emails,
                is_multiple=True)

    message.template_id = 'd-c8d1a24b4539459b831407e2562045a9'
    try:
        sg = SendGridAPIClient('API KEY')
        response = sg.send(message)
        print(response.status_code)
        print(response.body)
        print(response.headers)
    except Exception as e:
        print(str(e))

例外

HTTP Error 400: Bad Request

技术细节

sendgrid-python版本:我使用“ pip install sendgrid”安装,其版本是6.3.1,所以我不认为我在使用“ sendgrid-python”。 python版本:Python 3.6.7

我尝试升级,这就是打印出来的内容:

pip install sendgrid --upgrade

Requirement already up-to-date: sendgrid in ./p37ve/lib/python3.6/site-packages (6.3.1)
Requirement already satisfied, skipping upgrade: python-http-client>=3.2.1 in ./p37ve/lib/python3.6/site-packages (from sendgrid) (3.2.7)

0 个答案:

没有答案
相关问题