Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created June 12, 2016 20:31
Show Gist options
  • Save JoshCheek/b89fb3fec39607312731f6d33216c548 to your computer and use it in GitHub Desktop.
Save JoshCheek/b89fb3fec39607312731f6d33216c548 to your computer and use it in GitHub Desktop.
Record Email with AWS Lambda (they don't seem to include the body, in the end I got it by saving to an S3 bucket)
'use strict';
// When selecting the fn, you have two ways to call it:
// "Event" - asynchronous invocation
// "RequestResponse" - Logs the return value (maybe does more, but it cares about the return value, at the very least)
exports.handler = (event, context, callback) => {
/*
event:
{ Records: [{eventSource: 'aws:ses', eventVersion: '1.0', ses: <EMAIL>}]}
https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html
context:
{ callbackWaitsForEmptyEventLoop: [Getter/Setter],
done: [Function],
succeed: [Function],
fail: [Function],
logGroupName: '/aws/lambda/test',
logStreamName: '2016/06/12/[$LATEST]36232f56df424ab5bcb67524024bbc2d',
functionName: 'test',
memoryLimitInMB: '128',
functionVersion: '$LATEST',
getRemainingTimeInMillis: [Function],
invokeid: 'b24fc991-30c6-11e6-8af8-efdeb5bde635',
awsRequestId: 'b24fc991-30c6-11e6-8af8-efdeb5bde635',
invokedFunctionArn: 'arn:aws:lambda:us-west-2:737363147724:function:test'
}
<EMAIL>:
{ "eventVersion": "1.0",
"eventSource": "aws:ses",
"ses": {
"mail": {
"commonHeaders": {
"from": ["Jane Doe <janedoe@example.com>"],
"to": ["[email protected]"],
"returnPath": "[email protected]",
"messageId": "<0123456789example.com>",
"date": "Wed, 7 Oct 2015 12:34:56 -0700",
"subject": "Test Subject"
},
"source": "[email protected]",
"timestamp": "1970-01-01T00:00:00.000Z",
"destination": ["[email protected]"],
"messageId": "o3vrnil0e2ic28trm7dfhrc2v0clambda4nbp0g1",
"headersTruncated": false,
"headers": [
{ "name": "Return-Path", "value": "<janedoe@example.com>" },
{ "name": "Received", "value": "from mailer.example.com (mailer.example.com [203.0.113.1]) by inbound-smtp.us-west-2.amazonaws.com with SMTP id o3vrnil0e2ic28trm7dfhrc2v0cnbeccl4nbp0g1 for johndoe@example.com; Wed, 07 Oct 2015 12:34:56 +0000 (UTC)" },
{ "name": "DKIM-Signature", "value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; s=example; h=mime-version:from:date:message-id:subject:to:content-type; bh=jX3F0bCAI7sIbkHyy3mLYO28ieDQz2R0P8HwQkklFj4=; b=sQwJ+LMe9RjkesGu+vqU56asvMhrLRRYrWCbVt6WJulueecwfEwRf9JVWgkBTKiL6m2hr70xDbPWDhtLdLO+jB3hzjVnXwK3pYIOHw3vxG6NtJ6o61XSUwjEsp9tdyxQjZf2HNYee873832l3K1EeSXKzxYk9Pwqcpi3dMC74ct9GukjIevf1H46hm1L2d9VYTL0LGZGHOAyMnHmEGB8ZExWbI+k6khpurTQQ4sp4PZPRlgHtnj3Zzv7nmpTo7dtPG5z5S9J+L+Ba7dixT0jn3HuhaJ9b+VThboo4YfsX9PMNhWWxGjVksSFOcGluPO7QutCPyoY4gbxtwkN9W69HA==" },
{ "name": "MIME-Version", "value": "1.0" },
{ "name": "From", "value": "Jane Doe <janedoe@example.com>" },
{ "name": "Date", "value": "Wed, 7 Oct 2015 12:34:56 -0700" },
{ "name": "Message-ID", "value": "<0123456789example.com>" },
{ "name": "Subject", "value": "Test Subject" },
{ "name": "To", "value": "johndoe@example.com" },
{ "name": "Content-Type", "value": "text/plain; charset=UTF-8" }
]
},
"receipt": {
"recipients": ["johndoe@example.com"],
"processingTimeMillis": 574,
"timestamp": "1970-01-01T00:00:00.000Z",
"spamVerdict": {"status": "PASS"},
"dkimVerdict": {"status": "PASS"},
"action": {"type": "Lambda", "invocationType": "Event", "functionArn": "arn:aws:lambda:us-west-2:012345678912:function:Example"},
"spfVerdict": {"status": "PASS"},
"virusVerdict": {"status": "PASS"}
}
}
}
*/
const records = event.Records
console.log('Num Records:', records.length) // 1 for my purposes, at least
const notification = records[0].ses
console.log("Mail to " + notification.mail.destination.join(", ") + ":", notification.mail) // the above structure, doesn't seem to include body, so IDK how to follow the link :(
callback(null)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment