SES Attachments with Python

Viewed 7327

I'm using python 2.7 and boto3. I can not figure out a way to add attachments to SES in python. The closest thing I found was this page.

So far what I have is this:

from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import boto3

# via http://codeadict.wordpress.com/2010/02/11/send-e-mails-with-attachment-in-python/
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = email
msg['To'] = other_email

# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'

# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)

# the attachment
part = MIMEApplication(open('cat.jpg', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='cat.jpg')
msg.attach(part)

result = ses.send_raw_email(
    Source=msg['From'],
    Destinations=msg['To'],
    RawMessage=msg
)                                                                                                       
# and send the message
print result

And i get:

ParamValidationError: Parameter validation failed:
Invalid type for parameter RawMessage, value: From nobody Tue Jul 25 11:21:41 2017
Content-Type: multipart/mixed; boundary="===============0285276385=="
MIME-Version: 1.0
Subject: weekly report
From: email
To: other_email

"email" and "other_email" are censored but in String format 'e@mail.xxx'. The address is authorized through AWS and the Key and Secret key are already implemented through boto3.

Also got this at the bottom of the output:

type: <type 'instance'>, valid types: <type 'dict'>
Invalid type for parameter Destinations,
value: other_email, 
type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>
1 Answers
Related