使用 sendgrid 和 python,如何将一封电子邮件同时发送到多个密件抄送?
With sendgrid and python, how to send an email to multiple BCC at once?
拜托,在 python3 和 sendgrid 中,我需要以 BCC 方式向多个地址发送电子邮件。
我将这些电子邮件列在一个列表中。
我正在尝试使用个性化:
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, From, To, Cc, Bcc
recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']
new_email = Mail(from_email='emailsender@gmail.com',
to_emails = 'one_valid_email@gmail.com',
subject= "email subject",
html_content="Hi<br><br>This is a test")
personalization = Personalization()
for bcc_addr in recips:
personalization.add_bcc(Bcc(bcc_addr))
new_email.add_personalization(personalization)
try:
sg = SendGridAPIClient('API_KEY')
response = sg.send(new_email)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.to_dict)
在使用真实电子邮件地址的测试中出现错误:HTTP 错误 400:错误请求,带有字典:{'errors': [{'message': 'The to array is required for all personalization objects, and must have at least one email object with a valid email address.', 'field': 'personalizations.0.to', 'help': 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to'}]}
请问有人知道为什么吗?
此处为 Twilio SendGrid 开发人员布道师。
向您的个性化对象添加多个密件抄送时,您需要遍历电子邮件地址并分别添加它们。
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, Bcc, To
recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']
new_email = Mail(
from_email='emailsender@gmail.com',
subject= "email subject",
html_content="Hi<br><br>This is a test"
)
personalization = Personalization()
personalization.add_to(To('emailsender@gmail.com'))
for bcc_addr in recips:
personalization.add_bcc(Bcc(bcc_addr))
new_email.add_personalization(personalization)
try:
sg = SendGridAPIClient('API_KEY')
response = sg.send(new_email)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.to_dict)
查看此 mail example 以了解如何使用个性化的各个部分。
拜托,在 python3 和 sendgrid 中,我需要以 BCC 方式向多个地址发送电子邮件。 我将这些电子邮件列在一个列表中。 我正在尝试使用个性化:
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, From, To, Cc, Bcc
recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']
new_email = Mail(from_email='emailsender@gmail.com',
to_emails = 'one_valid_email@gmail.com',
subject= "email subject",
html_content="Hi<br><br>This is a test")
personalization = Personalization()
for bcc_addr in recips:
personalization.add_bcc(Bcc(bcc_addr))
new_email.add_personalization(personalization)
try:
sg = SendGridAPIClient('API_KEY')
response = sg.send(new_email)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.to_dict)
在使用真实电子邮件地址的测试中出现错误:HTTP 错误 400:错误请求,带有字典:{'errors': [{'message': 'The to array is required for all personalization objects, and must have at least one email object with a valid email address.', 'field': 'personalizations.0.to', 'help': 'http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.personalizations.to'}]}
请问有人知道为什么吗?
此处为 Twilio SendGrid 开发人员布道师。
向您的个性化对象添加多个密件抄送时,您需要遍历电子邮件地址并分别添加它们。
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, Bcc, To
recips = ['email1@gmail.com', 'email2@gmail.com', 'email2@gmail.com']
new_email = Mail(
from_email='emailsender@gmail.com',
subject= "email subject",
html_content="Hi<br><br>This is a test"
)
personalization = Personalization()
personalization.add_to(To('emailsender@gmail.com'))
for bcc_addr in recips:
personalization.add_bcc(Bcc(bcc_addr))
new_email.add_personalization(personalization)
try:
sg = SendGridAPIClient('API_KEY')
response = sg.send(new_email)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.to_dict)
查看此 mail example 以了解如何使用个性化的各个部分。