Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto SES - send_raw_email() to multiple recipients

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 = '[email protected]'
    recipients = ['[email protected]', '[email protected]', '[email protected]']
    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 <[email protected]>'        

    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?

like image 506
Jared Avatar asked Apr 15 '15 20:04

Jared


Video Answer


1 Answers

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 = '[email protected]'
    recipients = ['[email protected]', '[email protected]', '[email protected]']
    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 <[email protected]>' 
    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)
like image 153
chamal sapumohotti Avatar answered Sep 29 '22 23:09

chamal sapumohotti