Skip to content

Instantly share code, notes, and snippets.

@voduytuan
Last active July 16, 2016 04:06
Show Gist options
  • Save voduytuan/b9b9a5ef5fbc079f1920160b7e24b893 to your computer and use it in GitHub Desktop.
Save voduytuan/b9b9a5ef5fbc079f1920160b7e24b893 to your computer and use it in GitHub Desktop.
ses-receiving-lambda.js
'use strict';
let AWS = require('aws-sdk');
// We need this to build our post string
let http = require('http');
let fs = require('fs');
function makePostRequest(data) {
// Build the post string from an object
var post_data = JSON.stringify(data);
// An object of options to indicate where to post to
var post_options = {
host: 'requestb.in',
port: '80',
path: '/1c38kdc1',
method: 'POST',
headers: {
'Content-Type': 'application/application/json'
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('POST Response: ' + chunk);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
exports.handler = (event, context, callback) => {
console.log('Spam filter starting');
const sesNotification = event.Records[0].ses;
const messageId = sesNotification.mail.messageId;
const receipt = sesNotification.receipt;
console.log('Processing message:', messageId);
// Check if any spam check failed
if (receipt.spfVerdict.status === 'FAIL' ||
receipt.dkimVerdict.status === 'FAIL' ||
receipt.spamVerdict.status === 'FAIL' ||
receipt.virusVerdict.status === 'FAIL') {
let sendBounceParams = {
BounceSender: 'mailer-daemon@<MYDOMAIN>.com',
OriginalMessageId: messageId,
MessageDsn: {
ReportingMta: 'dns; <MYDOMAIN>.com',
ArrivalDate: new Date(),
ExtensionFields: []
},
BouncedRecipientInfoList: receipt.recipients.map((recipient) => ({
Recipient: recipient,
BounceType: 'ContentRejected'
}))
};
console.log('Bouncing message with parameters:');
console.log(JSON.stringify(sendBounceParams, null, 2));
new AWS.SES().sendBounce(sendBounceParams, (err, data) => {
if (err) {
console.log(`An error occurred while sending bounce for message: ${messageId}`, err);
callback(err);
} else {
console.log(`Bounce for message ${messageId} sent, bounce message ID: ${data.MessageId}`);
callback(null, {
disposition: 'stop_rule_set'
});
}
});
} else {
console.log('Accepting message:', messageId);
makePostRequest(sesNotification);
callback(null);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment