在 python 3 和 sendgrid 中,我需要發送 BCC 型別的電子郵件并使用我
這里是這個地方的 HTML 代碼,Edit Module HTML:

uj5u.com熱心網友回復:
那是因為您試圖呼叫物件add_personalization上的方法to_emails,這是您在上面幾行定義的串列:
to_emails = [To(email= '[email protected]']
您需要呼叫您創建add_personalization的message物件,如下所示:
message.add_personalization(personalization)
這是修復的完整代碼:
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Attachment, Mail
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException, Personalization, Bcc
import os
API_KEY = "real id"
lista = { "sentences": ["Sentence 1 <br>", "Sentence 2 <br>"] }
recips = ['[email protected]', '[email protected]', '[email protected]']
to_emails = [To(email= '[email protected]']
personalization = Personalization()
personalization.add_to(To('[email protected]'))
for bcc_addr in recips:
personalization.add_bcc(Bcc(bcc_addr))
message = Mail(
from_email=('[email protected]'),
subject="email subject",
to_emails=to_emails,
is_multiple=True)
message.add_personalization(personalization)
message.dynamic_template_data = lista
message.template_id = 'real id'
try:
sendgrid_client = SendGridAPIClient(api_sendgrid)
response = sendgrid_client.send(message)
print(response.status_code)
#print(response.body)
#print(response.headers)
except Exception as e:
print(e.message)
return
由于您的“句子”動態模板資料是一個陣列,因此您也應該回圈遍歷它,以便列印每個句子。在你的模板中試試這個:
{{#each sentences}}
<div style="font-family: inherit; text-align: inherit">{{this}}</div>
{{/each}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/441462.html
上一篇:SendGrid中的組合把手
