Receive and Parse Emails on AWS SES












3















I want to setup a Lambda function to parse incoming emails to SES. I followed the documentation and setup the receipt rules.



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.



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()









share|improve this question





























    3















    I want to setup a Lambda function to parse incoming emails to SES. I followed the documentation and setup the receipt rules.



    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.



    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()









    share|improve this question



























      3












      3








      3


      1






      I want to setup a Lambda function to parse incoming emails to SES. I followed the documentation and setup the receipt rules.



      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.



      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()









      share|improve this question
















      I want to setup a Lambda function to parse incoming emails to SES. I followed the documentation and setup the receipt rules.



      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.



      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()






      python aws-lambda amazon-ses






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 5 '16 at 2:38









      Laurel

      4,773102137




      4,773102137










      asked Aug 29 '16 at 23:48









      Yolo49Yolo49

      81312




      81312
























          3 Answers
          3






          active

          oldest

          votes


















          3














          You can set up an Action in your SES Rules Set to automatically PUT your email files into S3. Then you set up an event in S3 (for a specific bucket) to trigger your lambda function. With that, you will be able to retrieve the email with something like this:



          def lambda_handler(event, context):

          for record in event['Records']:
          key = record['s3']['object']['key']
          bucket = record['s3']['bucket']['name']
          # here you can download the file from s3 with bucket and key





          share|improve this answer

































            0














            See Amazon Simple Email Service document



            Actually, there is a better way; using boto3, you can send email and process messages easily.



            # 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()





            share|improve this answer

































              0














              It appears that joarleymoraes suggested the mult-part solution you are looking for. I'll try to further elaborate on this process. First, you'll want to utilize S3 Action within Simple Email Service.





              1. SES S3 Action - PUT your email to an S3 bucket


              Second, after the S3 Action (within the same SES receipt rule as your S3 Action) schedule your S3 email processing Lambda Action trigger.





              1. Lambda Action - GET from S3 and process the email contents


              The AWS SES documentation shows "Lambda Function Example #4" demonstrating the steps required to obtain your email from S3:



              var AWS = require('aws-sdk');
              var s3 = new AWS.S3();

              var bucketName = '<YOUR BUCKET GOES HERE>';

              exports.handler = function(event, context, callback) {
              console.log('Process email');

              var sesNotification = event.Records[0].ses;
              console.log("SES Notification:n", JSON.stringify(sesNotification, null, 2));

              // Retrieve the email from your bucket
              s3.getObject({
              Bucket: bucketName,
              Key: sesNotification.mail.messageId
              }, function(err, data) {
              if (err) {
              console.log(err, err.stack);
              callback(err);
              } else {
              console.log("Raw email:n" + data.Body);

              // Custom email processing goes here

              callback(null, null);
              }
              });
              };





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39216839%2freceive-and-parse-emails-on-aws-ses%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                You can set up an Action in your SES Rules Set to automatically PUT your email files into S3. Then you set up an event in S3 (for a specific bucket) to trigger your lambda function. With that, you will be able to retrieve the email with something like this:



                def lambda_handler(event, context):

                for record in event['Records']:
                key = record['s3']['object']['key']
                bucket = record['s3']['bucket']['name']
                # here you can download the file from s3 with bucket and key





                share|improve this answer






























                  3














                  You can set up an Action in your SES Rules Set to automatically PUT your email files into S3. Then you set up an event in S3 (for a specific bucket) to trigger your lambda function. With that, you will be able to retrieve the email with something like this:



                  def lambda_handler(event, context):

                  for record in event['Records']:
                  key = record['s3']['object']['key']
                  bucket = record['s3']['bucket']['name']
                  # here you can download the file from s3 with bucket and key





                  share|improve this answer




























                    3












                    3








                    3







                    You can set up an Action in your SES Rules Set to automatically PUT your email files into S3. Then you set up an event in S3 (for a specific bucket) to trigger your lambda function. With that, you will be able to retrieve the email with something like this:



                    def lambda_handler(event, context):

                    for record in event['Records']:
                    key = record['s3']['object']['key']
                    bucket = record['s3']['bucket']['name']
                    # here you can download the file from s3 with bucket and key





                    share|improve this answer















                    You can set up an Action in your SES Rules Set to automatically PUT your email files into S3. Then you set up an event in S3 (for a specific bucket) to trigger your lambda function. With that, you will be able to retrieve the email with something like this:



                    def lambda_handler(event, context):

                    for record in event['Records']:
                    key = record['s3']['object']['key']
                    bucket = record['s3']['bucket']['name']
                    # here you can download the file from s3 with bucket and key






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 6 '16 at 19:08

























                    answered Sep 5 '16 at 14:18









                    joarleymoraesjoarleymoraes

                    971711




                    971711

























                        0














                        See Amazon Simple Email Service document



                        Actually, there is a better way; using boto3, you can send email and process messages easily.



                        # 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()





                        share|improve this answer






























                          0














                          See Amazon Simple Email Service document



                          Actually, there is a better way; using boto3, you can send email and process messages easily.



                          # 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()





                          share|improve this answer




























                            0












                            0








                            0







                            See Amazon Simple Email Service document



                            Actually, there is a better way; using boto3, you can send email and process messages easily.



                            # 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()





                            share|improve this answer















                            See Amazon Simple Email Service document



                            Actually, there is a better way; using boto3, you can send email and process messages easily.



                            # 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()






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 30 '16 at 4:13









                            tripleee

                            89.8k13125183




                            89.8k13125183










                            answered Aug 30 '16 at 0:44









                            McGradyMcGrady

                            6,013102237




                            6,013102237























                                0














                                It appears that joarleymoraes suggested the mult-part solution you are looking for. I'll try to further elaborate on this process. First, you'll want to utilize S3 Action within Simple Email Service.





                                1. SES S3 Action - PUT your email to an S3 bucket


                                Second, after the S3 Action (within the same SES receipt rule as your S3 Action) schedule your S3 email processing Lambda Action trigger.





                                1. Lambda Action - GET from S3 and process the email contents


                                The AWS SES documentation shows "Lambda Function Example #4" demonstrating the steps required to obtain your email from S3:



                                var AWS = require('aws-sdk');
                                var s3 = new AWS.S3();

                                var bucketName = '<YOUR BUCKET GOES HERE>';

                                exports.handler = function(event, context, callback) {
                                console.log('Process email');

                                var sesNotification = event.Records[0].ses;
                                console.log("SES Notification:n", JSON.stringify(sesNotification, null, 2));

                                // Retrieve the email from your bucket
                                s3.getObject({
                                Bucket: bucketName,
                                Key: sesNotification.mail.messageId
                                }, function(err, data) {
                                if (err) {
                                console.log(err, err.stack);
                                callback(err);
                                } else {
                                console.log("Raw email:n" + data.Body);

                                // Custom email processing goes here

                                callback(null, null);
                                }
                                });
                                };





                                share|improve this answer




























                                  0














                                  It appears that joarleymoraes suggested the mult-part solution you are looking for. I'll try to further elaborate on this process. First, you'll want to utilize S3 Action within Simple Email Service.





                                  1. SES S3 Action - PUT your email to an S3 bucket


                                  Second, after the S3 Action (within the same SES receipt rule as your S3 Action) schedule your S3 email processing Lambda Action trigger.





                                  1. Lambda Action - GET from S3 and process the email contents


                                  The AWS SES documentation shows "Lambda Function Example #4" demonstrating the steps required to obtain your email from S3:



                                  var AWS = require('aws-sdk');
                                  var s3 = new AWS.S3();

                                  var bucketName = '<YOUR BUCKET GOES HERE>';

                                  exports.handler = function(event, context, callback) {
                                  console.log('Process email');

                                  var sesNotification = event.Records[0].ses;
                                  console.log("SES Notification:n", JSON.stringify(sesNotification, null, 2));

                                  // Retrieve the email from your bucket
                                  s3.getObject({
                                  Bucket: bucketName,
                                  Key: sesNotification.mail.messageId
                                  }, function(err, data) {
                                  if (err) {
                                  console.log(err, err.stack);
                                  callback(err);
                                  } else {
                                  console.log("Raw email:n" + data.Body);

                                  // Custom email processing goes here

                                  callback(null, null);
                                  }
                                  });
                                  };





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    It appears that joarleymoraes suggested the mult-part solution you are looking for. I'll try to further elaborate on this process. First, you'll want to utilize S3 Action within Simple Email Service.





                                    1. SES S3 Action - PUT your email to an S3 bucket


                                    Second, after the S3 Action (within the same SES receipt rule as your S3 Action) schedule your S3 email processing Lambda Action trigger.





                                    1. Lambda Action - GET from S3 and process the email contents


                                    The AWS SES documentation shows "Lambda Function Example #4" demonstrating the steps required to obtain your email from S3:



                                    var AWS = require('aws-sdk');
                                    var s3 = new AWS.S3();

                                    var bucketName = '<YOUR BUCKET GOES HERE>';

                                    exports.handler = function(event, context, callback) {
                                    console.log('Process email');

                                    var sesNotification = event.Records[0].ses;
                                    console.log("SES Notification:n", JSON.stringify(sesNotification, null, 2));

                                    // Retrieve the email from your bucket
                                    s3.getObject({
                                    Bucket: bucketName,
                                    Key: sesNotification.mail.messageId
                                    }, function(err, data) {
                                    if (err) {
                                    console.log(err, err.stack);
                                    callback(err);
                                    } else {
                                    console.log("Raw email:n" + data.Body);

                                    // Custom email processing goes here

                                    callback(null, null);
                                    }
                                    });
                                    };





                                    share|improve this answer













                                    It appears that joarleymoraes suggested the mult-part solution you are looking for. I'll try to further elaborate on this process. First, you'll want to utilize S3 Action within Simple Email Service.





                                    1. SES S3 Action - PUT your email to an S3 bucket


                                    Second, after the S3 Action (within the same SES receipt rule as your S3 Action) schedule your S3 email processing Lambda Action trigger.





                                    1. Lambda Action - GET from S3 and process the email contents


                                    The AWS SES documentation shows "Lambda Function Example #4" demonstrating the steps required to obtain your email from S3:



                                    var AWS = require('aws-sdk');
                                    var s3 = new AWS.S3();

                                    var bucketName = '<YOUR BUCKET GOES HERE>';

                                    exports.handler = function(event, context, callback) {
                                    console.log('Process email');

                                    var sesNotification = event.Records[0].ses;
                                    console.log("SES Notification:n", JSON.stringify(sesNotification, null, 2));

                                    // Retrieve the email from your bucket
                                    s3.getObject({
                                    Bucket: bucketName,
                                    Key: sesNotification.mail.messageId
                                    }, function(err, data) {
                                    if (err) {
                                    console.log(err, err.stack);
                                    callback(err);
                                    } else {
                                    console.log("Raw email:n" + data.Body);

                                    // Custom email processing goes here

                                    callback(null, null);
                                    }
                                    });
                                    };






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 19 at 5:37









                                    taky2taky2

                                    345414




                                    345414






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39216839%2freceive-and-parse-emails-on-aws-ses%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Callistus III

                                        Plistias Cous

                                        Index Sanctorum