Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.201.0

Using Receipt Rules in Amazon SES

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • Create and delete receipt rules.

  • Organize receipt rules into receipt rule sets.

Receipt rules in Amazon SES specify what to do with email received for email addresses or domains you own. A receipt rule contains a condition and an ordered list of actions. If the recipient of an incoming email matches a recipient specified in the conditions for the receipt rule, Amazon SES performs the actions that the receipt rule specifies.

To use Amazon SES as your email receiver, you must have at least one active receipt rule set. A receipt rule set is an ordered collection of receipt rules that specify what Amazon SES should do with mail it receives across your verified domains. For more information, see Creating Receipt Rules for Amazon SES Email Receiving and Creating a Receipt Rule Set for Amazon SES Email Receiving in the Amazon Simple Email Service Developer Guide.

The Scenario

In this example, a series of Node.js modules are used to send email in a variety of ways. The Node.js modules use the SDK for JavaScript to create and use email templates using these methods of the AWS.SES client class:

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Creating an Amazon S3 Receipt Rule

Each receipt rule for Amazon SES contains an ordered list of actions. This example creates a receipt rule with an Amazon S3 action, which delivers the mail message to an Amazon S3 bucket. For details on receipt rule actions, see Action Options in the Amazon Simple Email Service Developer Guide.

For Amazon SES to write email to an Amazon S3 bucket, create a bucket policy that gives PutObject permission to Amazon SES. For information about creating this bucket policy, see Give Amazon SES Permission to Write to Your Amazon S3 Bucket in the Amazon Simple Email Service Developer Guide.

In this example, use a Node.js module to create a receipt rule in Amazon SES to save received messages in an Amazon S3 bucket. Create a Node.js module with the file name ses_createreceiptrule.js. Configure the SDK as previously shown.

Create a parameters object to pass the values needed to create for the receipt rule set. To call the createReceiptRuleSet method, create a promise for invoking an Amazon SES service object, passing the parameters. Then handle the response in the promise callback.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create createReceiptRule params var params = { Rule: { Actions: [ { S3Action: { BucketName: "S3_BUCKET_NAME/*", ObjectKeyPrefix: "email" } } ], Recipients: [ 'DOMAIN | EMAIL_ADDRESS', /* more items */ ], Enabled: true | false, Name: "RULE_NAME", ScanEnabled: true | false, TlsPolicy: "Optional" }, RuleSetName: "RULE_SET_NAME" }; // Create the promise and SES service object var newRulePromise = new AWS.SES({apiVersion: '2010-12-01'}).createReceiptRule(params).promise(); // Handle promise's fulfilled/rejected states newRulePromise.then( function(data) { console.log("Rule created"); }).catch( function(err) { console.error(err, err.stack); });

To run the example, type the following at the command line. Amazon SES creates the receipt rule.

node ses_createreceiptrule.js

This sample code can be found here on GitHub.

Deleting a Receipt Rule

In this example, use a Node.js module to send email with Amazon SES. Create a Node.js module with the file name ses_deletereceiptrule.js. Configure the SDK as previously shown.

Create a parameters object to pass the name for the receipt rule to delete. To call the deleteReceiptRule method, create a promise for invoking an Amazon SES service object, passing the parameters. Then handle the response in the promise callback.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create deleteReceiptRule params var params = { RuleName: 'RULE_NAME', /* required */ RuleSetName: 'RULE_SET_NAME' /* required */ }; // Create the promise and SES service object var newRulePromise = new AWS.SES({apiVersion: '2010-12-01'}).deleteReceiptRule(params).promise(); // Handle promise's fulfilled/rejected states newRulePromise.then( function(data) { console.log("Receipt Rule Deleted"); }).catch( function(err) { console.error(err, err.stack); });

To run the example, type the following at the command line. Amazon SES creates the receipt rule set list.

node ses_deletereceiptrule.js

This sample code can be found here on GitHub.

Creating a Receipt Rule Set

In this example, use a Node.js module to send email with Amazon SES. Create a Node.js module with the file name ses_createreceiptruleset.js. Configure the SDK as previously shown.

Create a parameters object to pass the name for the new receipt rule set. To call the createReceiptRuleSet method, create a promise for invoking an Amazon SES service object, passing the parameters. Then handle the response in the promise callback.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the promise and SES service object var newRulePromise = new AWS.SES({apiVersion: '2010-12-01'}).createReceiptRuleSet({RuleSetName: "NAME"}).promise(); // Handle promise's fulfilled/rejected states newRulePromise.then( function(data) { console.log(data); }).catch function(err) { console.error(err, err.stack); });

To run the example, type the following at the command line. Amazon SES creates the receipt rule set list.

node ses_createreceiptruleset.js

This sample code can be found here on GitHub.

Deleting a Receipt Rule Set

In this example, use a Node.js module to send email with Amazon SES. Create a Node.js module with the file name ses_deletereceiptruleset.js. Configure the SDK as previously shown.

Create an object to pass the name for the receipt rule set to delete. To call the deleeReceiptRuleSet method, create a promise for invoking an Amazon SES service object, passing the parameters. Then handle the response in the promise callback.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the promise and SES service object var newRulePromise = new AWS.SES({apiVersion: '2010-12-01'}).deleeReceiptRuleSet({RuleSetName: "NAME"}).promise(); // Handle promise's fulfilled/rejected states newRulePromise.then( function(data) { console.log(data); }).catch( function(err) { console.error(err, err.stack); });

To run the example, type the following at the command line. Amazon SES creates the receipt rule set list.

node ses_deletereceiptruleset.js

This sample code can be found here on GitHub.