如何使用 sendgrid 和动态模板在 python 中发送带有密件抄送地址的电子邮件?

How to send email in python with sendgrid and dynamic template, with BCC addresses?

在 python 3 和 sendgrid 中,我需要发送 BCC 类型的电子邮件并使用我构建的动态模板 here

在动态模板中我放了一个空白 space 来接收我将要发送的数据。我在空白处创建了变量 {{lista}} space。在白色space模板的HTML中看起来是这样的:

<div style="font-family: inherit; text-align: inherit">{{sentences}}</div>

我的 python 代码如下所示:

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 = ["Sentence 1 <br>", "Sentence 2 <br>"]
lista = {"sentences": lista}

recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']

to_emails = [
        To(email= 'one_valid_email@gmail.com',
           dynamic_template_data = lista)] 

personalization = Personalization()
personalization.add_to(To('one_valid_email@gmail.com'))
    
for bcc_addr in recips:
        personalization.add_bcc(Bcc(bcc_addr))
   
message = Mail(
        from_email=('emailsender@gmail.com'),
        subject="email subject", 
        to_emails=to_emails,
        is_multiple=True)

message.add_personalization(personalization)

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

电子邮件已发送,但模板为空,没有“列表”数据

拜托,有人知道哪里出了问题吗?

这是我的模板图片,我创建空白以接收数据的地方:

这里是HTML代码的地方,编辑模块HTML:

那是因为您正试图在 to_emails 对象上调用方法 add_personalization,该对象是您在上面几行中定义的列表:

to_emails = [To(email= 'one_valid_email@gmail.com'] 

您需要调用 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 = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']

to_emails = [To(email= 'one_valid_email@gmail.com'] 

personalization = Personalization()
personalization.add_to(To('one_valid_email@gmail.com'))
    
for bcc_addr in recips:
        personalization.add_bcc(Bcc(bcc_addr))

message = Mail(
        from_email=('emailsender@gmail.com'),
        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}}