Harini P
Harini P

Reputation: 627

How to access the content of AWS SES email dumped in S3?

I am trying to access the content of an Email received through SES and dumped in S3.

I want to use a python 3.6 lambda function to parse the email and do operations. I tried

emailRawString = response.get()['Body'].read()

But was hit with the following error:

get expected at least 1 arguments, got 0

I can see the content type in the s3 bucket of the dumped MIME file is: application/octet-stream

Any help will be appreciated. I just want to open and get the MIME contents so that I can parse and do operations based on that.

Note: The rest of the script is fine.

Edit: The actual code I am using:

response = s3.get_object(Bucket=bucket, Key=key)

        emailRawString = response.get()['Body'].read()
        parser = Parser()
        emailString = parser.parsestr(emailRawString)
        toAddress = emailString.get('To').split(",")
        if emailString.is_multipart():
            for part in emailString.walk():
                if part.get_content_type() == 'text/plain':
                    body = part.get_payload()
        else:
            body = emailString.get_payload()

Upvotes: 4

Views: 2523

Answers (1)

Steve E.
Steve E.

Reputation: 9353

The get() is not required.

Looking at the boto3 S3 example. This should work:

response = s3.get_object(Bucket=bucket, Key=key)

emailRawString = response['Body'].read()
parser = Parser()
emailString = parser.parsestr(emailRawString)
toAddress = emailString.get('To').split(",")
if emailString.is_multipart():
    for part in emailString.walk():
        if part.get_content_type() == 'text/plain':
            body = part.get_payload()
else:
    body = emailString.get_payload()

Upvotes: 1

Related Questions