gpt4 book ai didi

python - Boto SES - send_raw_email() 给多个收件人

转载 作者:太空狗 更新时间:2023-10-29 21:26:21 25 4
gpt4 key购买 nike

我在这个问题上遇到了很大的问题——另一个没有解决的关于 SO 的问题在这里:Send Raw Email (with attachment) to Multiple Recipients

我的代码(有效)很简单:

def send_amazon_email_with_attachment(html, subject, now, pre):
dummy = 'test@example.com'
recipients = ['test1@exampl.ecom', 'test2@example.com', 'test3@example.com']
connS3 = S3Connection('IDENTIFICATION','PASSWORD')
b = connS3.get_bucket('BUCKET_NAME')
key = b.get_key('FILE_NAME.pdf')
temp = key.get_contents_as_string()

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = 'My Name <test@example.com>'

msg.preamble = 'Multipart message.\n'

part1 = MIMEText(u"Attached is the report", 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

part = MIMEApplication(temp) #read binary
part.add_header('Content-Disposition', 'attachment', filename='FILE_NAME.pdf')
msg.attach(part)

conn = boto.ses.connect_to_region('us-east-1', aws_access_key_id='AUTH_ID', aws_secret_access_key='AUTH_PW')
for recipient in recipients:
print recipient
msg['To'] = recipient

result = conn.send_raw_email(msg.as_string(), source=msg['From'], destinations=recipient)

但是,有一个警告......这是为每个收件人循环。此的任何变体都不起作用。将列表传递给 msg['Bcc']msg['BCC'] 将返回无法删除列表的错误(与发布的完全相同的错误问题)。传递以逗号分隔的字符串会导致 Amazon SES 问题,在返回的 XML 中显示“非法电子邮件”。因为我只在特定情况下从亚马逊收到错误,所以我被引导相信这是程序在调用他们的 API 之前的错误。

MIMEMultipart 专家有什么想法吗?

最佳答案

基本上,您需要使用 2 种不同的格式在 2 个不同的地方指定电子邮件收件人。

def send_amazon_email_with_attachment(html, subject, now, pre):
dummy = 'test@example.com'
recipients = ['test1@exampl.ecom', 'test2@example.com', 'test3@example.com']
connS3 = S3Connection('IDENTIFICATION','PASSWORD')
b = connS3.get_bucket('BUCKET_NAME')
key = b.get_key('FILE_NAME.pdf')
temp = key.get_contents_as_string()

msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = 'My Name <test@example.com>'
msg['To'] = ', '.join(recipients)


msg.preamble = 'Multipart message.\n'

part1 = MIMEText(u"Attached is the report", 'plain')
part2 = MIMEText(html, 'html')

msg.attach(part1)
msg.attach(part2)

part = MIMEApplication(temp) #read binary
part.add_header('Content-Disposition', 'attachment', filename='FILE_NAME.pdf')
msg.attach(part)

conn = boto.ses.connect_to_region('us-east-1', aws_access_key_id='AUTH_ID', aws_secret_access_key='AUTH_PW')


result = conn.send_raw_email(msg.as_string(), source=msg['From'], destinations=recipients)

关于python - Boto SES - send_raw_email() 给多个收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29660366/

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