diff --git a/configuration/mail_forward.js b/configuration/mail_forward.js new file mode 100644 index 00000000000..6bc037db681 --- /dev/null +++ b/configuration/mail_forward.js @@ -0,0 +1,38 @@ +// Taken from https://github.com/sirceljm/AWS-SES-simple-mail-forward/blob/master/mail_forward.js +// Install as an AWS Lambda according to https://github.com/sirceljm/AWS-SES-simple-mail-forward +// and configure the to_address environment variable as a JSON array of strings representing +// the recipients' e-mail addresses. Subscribe the lambda to your SNS topic that receives the +// notification messages about your inbound e-mail. Use mail_forward_policy.json for the IAM role's +// security policy. +var AWS = require("aws-sdk"); +var ses = new AWS.SES(); + +exports.handler = (event, context, callback) => { + let data = JSON.parse(event.Records[0].Sns.Message); + let source = data.content; + + source = source.replace(/^DKIM-Signature/im, "X-Original-DKIM-Signature"); + // source = source.replace(/^From/im, "X-Original-From"); + // source = source.replace(/^Source/im, "X-Original-Source"); + // source = source.replace(/^Sender/im, "X-Original-Sender"); + // source = source.replace(/^Return-Path/im, "X-Original-Return-Path"); + source = source.replace(/^Domainkey-Signature/im, "X-Original-Domainkey-Signature"); + // source = `From: AWS SES Mail Forwarding <${data.mail.destination}>\n` + source; + + if (!process.env.to_address) { + console.log("You need to supply to_address enviromnent variable"); + } else if (data.mail.destination == process.env.to_address) { + console.log("MAIL SEND ABORTED! - Forwarded mail address and to_address are the same - THIS CAN CAUSE AN INFINITE LOOP!"); + } else { + ses.sendRawEmail({ + RawMessage: { + Data: source, + }, + Destinations: JSON.parse(process.env.to_address), + }, function (err, data) { + if (err) { + console.error(err, err.stack); + } + }); + } +}; diff --git a/configuration/mail_forward_policy.json b/configuration/mail_forward_policy.json new file mode 100644 index 00000000000..e586059e9e8 --- /dev/null +++ b/configuration/mail_forward_policy.json @@ -0,0 +1,22 @@ +{ + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ], + "Resource": "arn:aws:logs:*:*:*" + }, + { + "Effect": "Allow", + "Action": [ + "ses:SendEmail", + "ses:SendRawEmail" + ], + "Resource": "*" + } + ] +} diff --git a/home.md b/home.md index f49b2da2c1e..65c8056a42f 100644 --- a/home.md +++ b/home.md @@ -35,6 +35,7 @@ SAP is at the center of today’s technology revolution, developing innovations * [[Creating an EC2 image from scratch|wiki/info/landscape/creating-ec2-image-from-scratch]] * [[Creating a webserver EC2 image from scratch|wiki/info/landscape/creating-ec2-image-for-webserver-from-scratch]] * [[EC2 mail relaying vs. Amazon Simple E-Mail Service (SES)|wiki/info/landscape/mail-relaying]] + * [[Establishing support@sapsailing.com with AWS SES, SNS, and Lambda|wiki/info/landscape/support-email]] * [[Creating an EC2 image for a MongoDB Replica Set from scratch|wiki/info/landscape/creating-ec2-mongodb-image-from-scratch]] * [[Setting up dedicated S3 buckets|wiki/info/landscape/s3-bucket-setup]] * [[Building and Deploying|wiki/info/landscape/building-and-deploying]] diff --git a/wiki/info/landscape/support-email.md b/wiki/info/landscape/support-email.md new file mode 100644 index 00000000000..ffd8995cb44 --- /dev/null +++ b/wiki/info/landscape/support-email.md @@ -0,0 +1,15 @@ +# Set Up of Support E Mail Address support@sapsailing.com with AWS SES/SNS/Lambda + +We use AWS SES for all e-mail services, inbound and outbound. See also [Amazon Simple Email Service (SES) Set-Up](wiki/info/landscape/mail-relaying) for details on the general SES set-up. We have established ``support@sapsailing.com`` now as another inbound e-mail address that forwards to an SNS topic named ``Support-Mail``. + +The messages forwarded from SES to SNS are JSON-formatted wrappers around the original e-mail message augmented with various meta-data. Forwarding these messages with a straight SNS subscription of type EMAIL results in human-unreadable e-mails in JSON format with all the metadata included. + +Instead, we subscribe a Lambda function on the ``Support-Mail`` SNS topic that parses the original message from the JSON document and re-sends it through SES to a list of recipients that can be configured in the ``to_address`` environment variable used by the Lambda. The ``to_address`` environment variable needs to be a valid JSON array that contains the recipients' e-mail addresses as string objects. + +The role that is created to execute the Lambda needs to have a policy set that can access SES and write logs. See also https://github.com/sirceljm/AWS-SES-simple-mail-forward/blob/master/mail_forward_policy.json. + +The relevant files have been committed to our git as ``configuration/mail_forward.js`` for the Lambda's code, and as ``configuration/mail_forward_policy.json`` for the security policy for the IAM role used for the Lambda. + +## Changing the List of Recipients + +Go to [https://eu-west-1.console.aws.amazon.com/lambda/home?region=eu-west-1#/functions/email-forward?tab=configuration](https://eu-west-1.console.aws.amazon.com/lambda/home?region=eu-west-1#/functions/email-forward?tab=configuration) and edit the ``to_address`` environment variable accordingly. \ No newline at end of file