Use a Node Lambda function to parse an email stored in an AWS S3 Bucket by SES

2.7k Views Asked by At

Here's the section of the Node Lambda function that gets the email stored in S3. How do I just get the 'text/plain' content from the returned data object?

Do I need to include an NPM email parsing dependency with the lambda function (uploaded as .zip) or should I use some regex in the lambda to get the section I want? If so what would that look like?

exports.handler = function(event, context, callback) {

    var sesNotification = event.Records[0].ses;

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

            data

        }
    });

};
1

There are 1 best solutions below

0
On

It would be safer to use mailparser package for parsing.

const simpleParser = require('mailparser').simpleParser;

simpleParser(data, (err, mail)=>{
        console.log(mail.text);
})