Skip to content

Instantly share code, notes, and snippets.

@wparad
Created October 19, 2016 18:40
Show Gist options
  • Save wparad/4e2c1e1be65fdff72bfa216234af8f62 to your computer and use it in GitHub Desktop.
Save wparad/4e2c1e1be65fdff72bfa216234af8f62 to your computer and use it in GitHub Desktop.
var AWS = require('aws-sdk');
var forwardFrom = '[email protected]';
var forwardTo = '[email protected]';
exports.handler = function(event, context) {
var msgInfo = JSON.parse(event.Records[0].Sns.Message);
// don't process spam messages
if (msgInfo.receipt.spamVerdict.status === 'FAIL' || msgInfo.receipt.virusVerdict.status === 'FAIL') {
console.log('Message is spam or contains virus, ignoring.');
context.succeed();
}
var email = msgInfo.content;
console.log('Source email');
console.log(email);
var headers = "From: "+forwardFrom+"\r\n";
headers += "Reply-To: "+msgInfo.mail.commonHeaders.from[0]+"\r\n";
headers += "X-Original-To: "+msgInfo.mail.commonHeaders.to[0]+"\r\n";
headers += "To: "+forwardTo+"\r\n";
headers += "Subject: Fwd: "+msgInfo.mail.commonHeaders.subject+"\r\n";
if (email) {
var res;
res = email.match(/Content-Type:.+\s*boundary.*/);
if (res) {
headers += res[0]+"\r\n";
}
else {
res = email.match(/^Content-Type:(.*)/m);
if (res) {
headers += res[0]+"\r\n";
}
}
res = email.match(/^Content-Transfer-Encoding:(.*)/m);
if (res) {
headers += res[0]+"\r\n";
}
res = email.match(/^MIME-Version:(.*)/m);
if (res) {
headers += res[0]+"\r\n";
}
var splitEmail = email.split("\r\n\r\n");
splitEmail.shift();
email = headers+"\r\n"+splitEmail.join("\r\n\r\n");
}
else {
email = headers+"\r\n"+"Empty email";
}
console.log(email);
new AWS.SES().sendRawEmail({
RawMessage: { Data: email }
}, function(err, data) {
if (err) context.fail(err);
else {
console.log('Sent with MessageId: ' + data.MessageId);
context.succeed();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment