gpt4 book ai didi

python - 使用sendgrid和python,如何一次向多个BCC发送电子邮件?

转载 作者:行者123 更新时间:2023-12-03 08:09:57 24 4
gpt4 key购买 nike

拜托,在 python3 和 sendgrid 中,我需要以密件抄送的方式向多个地址发送电子邮件。我将这些电子邮件列在一个列表中。我正在尝试这样的个性化:

import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, Personalization, From, To, Cc, Bcc

recips = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="543139353d3865143339353d387a373b39" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62070f030b0e5022050f030b0e4c010d0f" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cf9f1fdf5f0aedcfbf1fdf5f0b2fff3f1" rel="noreferrer noopener nofollow">[email protected]</a>']

new_email = Mail(from_email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="16737b777f7a65737872736456717b777f7a3875797b" rel="noreferrer noopener nofollow">[email protected]</a>',
to_emails = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88e7e6edd7fee9e4e1ecd7ede5e9e1e4c8efe5e9e1e4a6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a>',
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': '所有个性化对象都需要 to 数组,并且必须具有 at至少一个具有有效电子邮件地址的电子邮件对象。', '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 = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="741119151d1845341319151d185a171b19" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedbd3dfd7d28cfed9d3dfd7d290ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7b1e161a1217493b1c161a121755181416" rel="noreferrer noopener nofollow">[email protected]</a>']

new_email = Mail(
from_email='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef8a828e86839c8a818b8a9daf88828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a>',
subject= "email subject",
html_content="Hi<br><br>This is a test"
)

personalization = Personalization()

personalization.add_to(To('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd98909c94918e989399988fbd9a909c9491d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>'))

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了解如何使用个性化的各个部分。

关于python - 使用sendgrid和python,如何一次向多个BCC发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71116589/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com