I would like to stop receiving email from a specific domain or email address to my SES identity. How can I do this? 

Follow these steps to create a Lambda function and create an SES Receipt Rule:

  • Create a Lambda function to process incoming email messages, evaluate the sender email address or domain, and drop the message if it matches a specific domain or email address. The function logs all email processed in CloudWatch Logs.
  • Create an SES receipt rule, before the default inbound rule, that routes all incoming email to the Lambda function.

Note: Introducing the Lambda action on the SES receipt rule does result in SES and Lambda charges.

Create the Lambda function

1.    Open the Amazon Lambda console and select Create a Lambda function (First-time Lambda users might need to choose “Get Started Now,” which directs you to the “function create” screen). If prompted to select a blueprint, choose Blank Function, and then choose Next.

2.    For Name, enter "SESReceiptRule" or another name that’s meaningful for you.

3.    For Description, add a meaningful description; for example, “processes incoming email”.

4.    For Runtime, choose Node.js 4.3.

5.    Enter the following sample code:

'use strict';

const AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    console.log('Blocking email filter starting');

    const sesNotification = event.Records[0].ses;
    const messageId = sesNotification.mail.messageId;
    const receipt = sesNotification.receipt;
    const mail = sesNotification.mail;  
 
    // Convert the environment variable into array. Clean spaces from it.
    var blockingListString = process.env.blockingList;
    blockingListString = blockingListString.replace(/\s/g,'');  
    var blockingListArray = blockingListString.split(",");

    // Check if the mail source matches with any of the email addresses or domains defined in the environment variable
    function isListed() {
        var length = blockingListArray.length;
        for(var i = 0; i < length; i++) {
            if (mail.source.endsWith(blockingListArray[i]))
                return true;
        }
        return false;
    }

    console.log('Processing message:', messageId);

        // Processing the message
    if (isListed()) {
            callback(null, {'disposition':'STOP_RULE_SET'});
            console.log('Rejecting messageId: ', messageId, ' - Source: ', mail.source, ' - Recipients: ',receipt.recipients,' - Subject: ', mail.commonHeaders['subject']);
    }
    else {
        console.log('Accepting messageId:', messageId, ' - Source: ', mail.source, ' - Recipients: ',receipt.recipients,' - Subject: ', mail.commonHeaders['subject']);
        callback();
    }
};

6.    In the Environment variables field, for Key, enter "blockingList".
Note: There is no limit to the number of environment variables you can create if the total size of the set does not exceed 4 KB. For additional information, see Rules for Naming Environment Variables.

7.    For Value, enter the email addresses and domains that you want to block, separated by commas. For example, "example.com, JohnDoe@example.com".
Note: You can edit the list of email addresses and domains as needed without changing the Lambda function code.

8.    In the Lambda function handler and role section, leave Handler as "index.handler", select Role to expand, and then choose Create new role from template(s).

9.    For Role name, enter a name that is meaningful to you, choose Next, and then choose Create function.

Create an SES receipt rule

1.    Open the SES console. In the navigation pane, choose Rule Sets, and then choose Create Rule.

2.    Choose Create a New Rule Set. For Rule set name, enter a name that is meaningful to you, and then choose Create a Rule Set.

3.    In the Recipients field, enter your domains or email identities, and then choose Next Step.
Note: Enter the email or domains that you want the rule applied to, not the domains or email that you want blocked.

4.    Expand Add Action and then choose Lambda. Expand Lambda function and then choose the function that you created.

5.    Choose RequestResponse in the Invocation type section, and then choose Next Step.

6.    For Rule name, enter a name that is meaningful to you, expand Insert after rule, choose <Beginning>,choose Next Step, and then choose Create Rule.

Check logs in CloudWatch

1.    Open the CloudWatch console and choose Logs from the navigation pane.

2.    In Log Groups, choose your Lambda function log group. For example, /aws/lambda/name_of_your_function.

3.    Choose the log stream you want to check, which displays the email messages and domains being processed by the Lambda function, similar to the following:

14:08:25 START RequestId: aa939984-1b9b-11e7-83d2-efc6877bdc9b Version: $LATEST
14:08:25 2017-04-07T14:08:25.957Z aa939984-1b9b-11e7-83d2-efc6877bdc9b Blocking email filter starting
14:08:25 2017-04-07T14:08:25.958Z aa939984-1b9b-11e7-83d2-efc6877bdc9b Processing message: jc0iurgrtkrsrs7f5pk0rsmf4r3q0poikdjfdi01
14:08:25 2017-04-07T14:08:25.959Z aa939984-1b9b-11e7-83d2-efc6877bdc9b Rejecting messageId: jc0iurgrtkrsrs7f5pk0rsmf4r3q0poikdjfdi01 - Source:
user@example.com - Recipients: [ 'user@domain.com' ] - Subject: This is an unwanted message
14:08:25 END RequestId: aa939984-1b9b-11e7-83d2-efc6877bdc9b

Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2017-08-11