Receive and Parse Emails on AWS SES
<p>I want to setup a Lambda function to parse incoming emails to SES. I followed the documentation and setup the receipt rules. </p>

<p>I tested out my script by storing a MIME email in a txt file, parsing the email, and storing required info in a JSON document to be stored in a database. Now, I'm unsure of how to access the received email from SES and pull the information into my Python script. Any help would be greatly appreciated. </p>

<pre><code>from email.parser import Parser
parser = Parser()

f = open('roundtripMime.txt', "r")
rawText = f.read()
incoming = Parser().parsestr(rawText)

subject = incoming
subjectList = subject.split("|")

#Get number
NumberList = subjectList[0].split()
Number = NumberList[2].strip("()")

#Get Name
fullNameList = subjectList[3].split("/")
firstName = fullNameList[1].strip()
lastName = fullNameList[0].strip()
</code></pre>

<p>See <a href="http://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email.html" rel="nofollow">Amazon Simple Email Service document</a></p>

<p>Actually, there is a better way; using <a href="http://boto3.readthedocs.io/en/latest/guide/sqs.html#processing-messages" rel="nofollow">boto3</a>, you can send email and process messages easily.</p>

<pre><code># Get the service resource
sqs = boto3.resource('sqs')

# Get the queue
queue = sqs.get_queue_by_name(QueueName='test')

# Process messages by printing out body and optional author name
for message in queue.receive_messages(MessageAttributeNames=['Author']):
    # Get the custom author message attribute if it was set
    author_text = ''
    if message.message_attributes is not None:
        author_name = message.message_attributes.get('Author').get('StringValue')
        if author_name:
            author_text = ' ({0})'.format(author_name)

    # Print out the body and author (if set)
    print('Hello, {0}!{1}'.format(message.body, author_text))

    # Let the queue know that the message is processed
    message.delete()
</code></pre>

python aws-lambda amazon-ses