Ben Hall
Ben Hall

Reputation: 51

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

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

        }
    });

};

Upvotes: 3

Views: 3071

Answers (1)

Prathamesh Jagtap
Prathamesh Jagtap

Reputation: 403

It would be safer to use mailparser package for parsing.

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

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

Upvotes: 6

Related Questions