Git Product home page Git Product logo

ses-catchall's Introduction

ses-catchall

This is a AWS Lambda that implements SES/Workmail Catchalls.

Workmail has no capability for sending emails to non-registered users to a default inbox. This Lambda-based solution makes it possible.

THEORY

The 'big idea' is to forward any emails that have an "unknown" email address to the selected "default" email address. This is accomplished using SES rules.

A rule is put just before the Workmail rule that creates an SNS message with the incoming email event and sends it to a custom Lambda.

The Lambda determines if the email is one of the already verified user emails. If it is already verified, it passes the event on to Workmail for delivery.

If the recipient is not a verified email, it will update the headers and forward it back to SES as a new email. First, it removes a number of headers (DKIM, etc...) which would interfere with forwarding the email, and sets the recipient to the defaultEmail (specificed in a config object). Also, because SES will not allow unverified senders to deliver email, the TO field is set to the adminEmail (eg: [email protected]). Finally, this new email is forwarded to SES using the SES.sendRawEmail(). As a convenience, the Reply-To field in the forwarded email is set to the original sender, so that when you click the "Reply" button in the email client, the To field is set appropriately.

The trick here is that the Lambda will screen all incoming emails and look for any sent from the adminEmail. It assumes that anything sent from the adminEmail was previously forwarded and is destined for a defaultEmail inbox. Since the To field was previously reset to the defaultEmail, Workmail dutifully deposits it there.

LIMITATIONS

  • There is a 10MB limit on attachments in SES. Despite Workmail's 25MB limit, SES will bounce any incoming emails with more that 10MB
  • There may also be some issues with multiple attachments.
  • UPDATE The SNS message queue has a limit of 256KB which severely limits the size of attachments

WARNINGS

  • Be careful to specify the correct defaultEmail and adminEmail.
  • If you mistype one of them, you could create a Lambda which infinitely replicates and forwards the same email.
  • If this happens - uncomment line 68 and DEPLOY the Lambda immediately. It will take some time, but the email storm will eventually subside
async function handler(SNSEvent, context, callback) {
    // return callback(null, { 'disposition': 'STOP_RULE' }); // uncomment this and DEPLOY to curtail an email storm
    let sesMsg = JSON.parse(SNSEvent.Records[0].Sns.Message); log(JSON.stringify(sesMsg, null, 2));
    let originalDestination = sesMsg.mail.destination[0]; log({ originalDestination });

WORKMAIL SETUP

LAMBDA SETUP

  1. In the LAMBDA CONSOLE...
  2. Click: CREATE FUNCTION
  3. "Author From Scratch"
  4. Function Name: ses-default-inbox
  5. Runtime: Node.js 14.x
  6. Architecture: x86_64
  7. Click: CREATE FUNCTION
  8. In the code, replace the index.js with the index.js from the repo
    1. Update the config object with your emails
    2. set defaultEmail to the default email user (eg: [email protected])
    3. set adminEmail to the admin user (eg: [email protected])
    4. set verifiedEmails to the list of users you've already created in Workmail. Email to these users will pass unchanged to Workmail
    5. Optionally add a list of FROM email addresses you'd like filtered out - this is a bit of a spam filter
const config = {
    defaultEmail: "[email protected]",
    adminEmail: "[email protected]",
    verifiedEmails: [
        "[email protected]"
    ],
    ignoreEmails: [
        "[email protected]"
    ]
}
  1. In the Configuration -> Permissions, click on the role name
    1. Expand the AWSLambdaBasicExecutionRole...
    2. Click Edit Policy
    3. Click JSON
    4. Add the following policy to the JSON
    5. Click REVIEW POLICY
    6. Click SAVE CHANGES
        {
            "Effect": "Allow",
            "Action": "ses:SendRawEmail",
            "Resource": "*"
        },

SES SETUP

  • in the SES HOME console...
  • Click Rule Sets...
  • Click VIEW ACTIVE RULE SET
  • Click on the Rule name for your Workmail
    • example: m-4dfb6326f56e4a06a6e...
  • Under actions, create a new action
    • Select "Add Action -> SNS"
    • Select SNS -> Encoding UTF-8
    • Select SNS topic -> Create a SNS Topic
      • Topic Name: <domain.name>-sns (example: agilefrontiers-sns)
      • Display Name: <domain.name>-sns (example: agilefrontiers-sns)
      • Click CREATE TOPIC
      • Click the round "UP ARROW" and move the rule above the "WORKMAIL" rule
    • Click SAVE RULE

SNS SETUP

  • in the SNS console...
  • Click TOPICS
  • Click your new SNS topic (eg: agilefrontiers-sns)
  • Click CREATE SUBSCRIPTION
    • Select Protocol -> AWS LAMBDA
    • Select Endpoint -> ....ses-default-inbox
    • Click CREATE SUBSCRIPTION
  • Click on the EDIT button
    • Under Access Policy...
    • add "Service": "ses.amazonaws.com", to the Statement.Principal JSON object
    • Click SAVE CHANGES
{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*",
        "Service": "ses.amazonaws.com"
      },
      "Action": [

TEST IT

  1. Back in the LAMBDA console...
    1. Click the TEST Down-arrow and select CONFIGURE TEST EVENT
    2. replace the JSON with the contents of test.json from the repo
    3. Click SAVE
    4. Click DEPLOY (see the green "Changes Deployed" indicator)
    5. Click TEST and see the results:
Test Event Name
test

Response
{
  "disposition": "CONTINUE"
}

Function Logs
START RequestId: aec50a9c-8080-4e30-9217-a8a7c5ea8c99 Version: $LATEST
2021-11-20T02:13:00.866Z	aec50a9c-8080-4e30-9217-a8a7c5ea8c99	INFO	{
  "mail": {
    "headers": [
      {
        "name": "From",
        "value": "[email protected] <[email protected]>"
      }
    ],
    "destination": [
      "[email protected]"
    ]
  },
  "content": "email content"
}
2021-11-20T02:13:00.872Z	aec50a9c-8080-4e30-9217-a8a7c5ea8c99	INFO	{ originalDestination: '[email protected]' }
2021-11-20T02:13:00.910Z	aec50a9c-8080-4e30-9217-a8a7c5ea8c99	INFO	{
  originalLabel: '[email protected]',
  originalFrom: '[email protected]'
}
2021-11-20T02:13:00.910Z	aec50a9c-8080-4e30-9217-a8a7c5ea8c99	INFO	Previously forwarded to default [email protected] from [email protected] by way of: [email protected]
END RequestId: aec50a9c-8080-4e30-9217-a8a7c5ea8c99
REPORT RequestId: aec50a9c-8080-4e30-9217-a8a7c5ea8c99	Duration: 65.32 ms	Billed Duration: 66 ms	Memory Size: 128 MB	Max Memory Used: 74 MB	Init Duration: 420.40 ms

Request ID
aec50a9c-8080-4e30-9217-a8a7c5ea8c99

DEBUG / CONSOLE LOG

  • You can mute the debugging info by comment out line 26 and uncommenting line 27.
function dummy() { }
const log = console.log;
// const log = dummy;

FINAL THOUGHTS

  • This is a bit of a hack.
  • It seems like a simple thing to implement and I'm surprised the AWS Workmail team hasn't implemented such a feature before now.
  • There may be some financial reasons since AWS charges $4.00 per month for each "user".
  • While you can create aliases, it is a bit of a headache to try and configure EVERY possible special-purpose email address as an alias.
  • Although, it may be an anti-spam feature to require all aliases to be pre-defined.
  • Best wishes, and Continued Success!

Greg Smith

ses-catchall's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.