Jared
Jared

Reputation: 3892

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?

Upvotes: 11

Views: 20514

Answers (2)

Rahul Adepu
Rahul Adepu

Reputation: 1

msg['To'] = ', '.join(recipients) isn't working for me (throws encoding error). Simply comment that line and your 'destinations' in "send_raw_email" should contain a list. That will work like a charm.

Upvotes: 0

chamal sapumohotti
chamal sapumohotti

Reputation: 301

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)

Upvotes: 22

Related Questions