我正在使用 python 通过 SendGrid 发送动态电子邮件模板。当我通过 Sendgrid UI 测试电子邮件时,收件箱中会显示正确的 HTML 电子邮件模板,但通过 python 发送电子邮件时会显示纯文本版本。代码如下:
import sendgrid
import os
from sendgrid.helpers.mail import *
from sendgrid import SendGridAPIClient
sg = sendgrid.SendGridAPIClient('MY_KEY_HERE')
from_email = Email("example@email.com")
to_email = To("example@email.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "and easy to do anywhere, even with Python")
mail = Mail(from_email, to_email, subject, content)
mail.template_id = "d-9e2812d070bb458e8d6cbd6c9b8922f6"
mail.dynamic_template_data = {
"first_name": "John",
"last_name": "Blacutt",
}
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
print(response.body)
print(response.headers)
这是纯文本电子邮件: