我正在嘗試使用 Django 中的 Sendgrid 動態模板發送批量電子郵件并收到此錯誤:The personalizations field is required and must have at least one personalization.
使用sendgrid 6.9.7
有誰看到我可能出錯的地方:
from django.conf import settings
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To
def send_mass_email():
to_emails = [
To(email='[email protected]',
dynamic_template_data={
"thing_i_want_personalized": 'hello email 1',
}),
To(email='[email protected]',
dynamic_template_data={
"thing_i_want_personalized": 'hello email 2',
}),
]
msg = Mail(
from_email='[email protected]>',
)
msg.to_emails = to_emails
msg.is_multiple = True
msg.template_id = "d-template_id"
try:
sendgrid_client = SendGridAPIClient(settings.SENDGRID_API_KEY)
response = sendgrid_client.send(msg)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e)
print(e.body)
return
輸出是
HTTP Error 400: Bad Request
b'{"errors":[{"message":"The personalizations field is required and must have at least one personalization.","field":"personalizations","help":"http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#-Personalizations-Errors"}]}'
uj5u.com熱心網友回復:
該類Mail在初始化期間使用初始值做了一些事情(它基于 設定私有內部值to_emails),因此您需要在初始化程式中傳遞to_emails和is_multiple而不是稍后:
from django.conf import settings
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, To
def send_mass_email():
to_emails = [
To(email='[email protected]',
dynamic_template_data={
"thing_i_want_personalized": 'hello email 1',
}),
To(email='[email protected]',
dynamic_template_data={
"thing_i_want_personalized": 'hello email 2',
}),
]
msg = Mail(
from_email='[email protected]>',
to_emails = to_emails,
is_multiple = True
)
msg.template_id = "d-template_id"
try:
sendgrid_client = SendGridAPIClient(settings.SENDGRID_API_KEY)
response = sendgrid_client.send(msg)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e)
print(e.body)
return
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/450240.html
