Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple SES incoming email handler
// Handle incoming SES email events, persist the message in S3 and forward to specified address
var AWS = require('aws-sdk');
var ses = new AWS.SES();
var s3Bucket = 'your-bucket-name-to-persist-email-data';
var yourEmail = 'yourname@gmail.com'; // your existing email account to forward emails to
exports.handler = (event, context, callback) => {
console.log(JSON.stringify(event));
try {
var messageData = event.Records[0].Sns.Message;
var messageId = event.Records[0].Sns.MessageId;
putObjectToS3(messageId, messageData);
try {
ses.sendRawEmail({
RawMessage: {
Data: JSON.parse(messageData).content
},
Destinations: [yourEmail],
}, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
});
} catch (e) {
console.log("Error ses RawMessage: " + e);
}
} catch (e) {
console.log("Error putObjectToS3: " + e);
}
}
function putObjectToS3(key, data){
var s3 = new AWS.S3();
var params = {
Bucket : s3Bucket,
Key : key,
Body : data
}
s3.putObject(params, function(err, data) {
if (err) {
console.log(err, err.stack);
}
else {
console.log(data);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment