Boto SES - send_raw_email() to multiple recipients

我是研究僧i 提交于 2019-12-09 17:15:49

问题


I'm having big time problems with this issue-- another question on SO that didn't solve it is here: Send Raw Email (with attachment) to Multiple Recipients

My code (that works) is simple:

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)

but, there's a caveat... this is looping for each recipient. Any variation of this does not work. Passing a list to msg['Bcc'] or msg['BCC'] will return an error that the list can't be stripped (same exact error as the posted question). Passing a string separated by commas gives an Amazon SES issue saying 'Illegal Email' in the returned XML. Because I only get an error from Amazon in specific situations, I'm led to believe this is an error with the program before it hits their API call.

Any MIMEMultipart experts have some ideas?


回答1:


Basically you need to specify the email recipients in 2 different places using 2 different formats.

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)


来源:https://stackoverflow.com/questions/29660366/boto-ses-send-raw-email-to-multiple-recipients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!