Using receipt rules in Amazon SES - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Using receipt rules in Amazon SES

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to create and delete receipt rules.

  • How to 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 SES client class:

Prerequisite tasks

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

  • Set up the project environment to run these Node TypeScript examples, and install the required Amazon SDK for JavaScript and third-party modules. Follow the instructions on GitHub.

  • Create a shared configurations file with your user credentials. For more information about providing a credentials JSON file, see Loading credentials in Node.js from the shared credentials file.

Important

These examples demonstrate how to import/export client service objects and command using ECMAScript6 (ES6).

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 libs directory, and create a Node.js module with the file name sesClient.js. Copy and paste the code below into it, which creates the Amazon SES client object. Replace REGION with your Amazon Region.

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

This example code can be found here on GitHub.

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 CreateReceiptRuleSetCommand method, invoke an Amazon SES service object, passing the parameters.

Note

Replace S3_BUCKET_NAME with the name of the Amazon S3 bucket, EMAIL_ADDRESS | DOMAIN with the email, or domain, RULE_NAME with the name of the rule, and RULE_SET_NAME with the name of the ruleset.

import { CreateReceiptRuleCommand, TlsPolicy } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "../../libs/utils/util-string.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const RULE_NAME = getUniqueName("RuleName"); const S3_BUCKET_NAME = getUniqueName("S3BucketName"); const createS3ReceiptRuleCommand = ({ bucketName, emailAddresses, name, ruleSet, }) => { return new CreateReceiptRuleCommand({ Rule: { Actions: [ { S3Action: { BucketName: bucketName, ObjectKeyPrefix: "email", }, }, ], Recipients: emailAddresses, Enabled: true, Name: name, ScanEnabled: false, TlsPolicy: TlsPolicy.Optional, }, RuleSetName: ruleSet, // Required }); }; const run = async () => { const s3ReceiptRuleCommand = createS3ReceiptRuleCommand({ bucketName: S3_BUCKET_NAME, emailAddresses: ["email@example.com"], name: RULE_NAME, ruleSet: RULE_SET_NAME, }); try { return await sesClient.send(s3ReceiptRuleCommand); } catch (err) { console.log("Failed to create S3 receipt rule.", err); return err; } };

To run the example, enter the following at the command prompt. Amazon SES creates the receipt rule.

node ses_createreceiptrule.js

This example 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, including installing the required clients and packages.

Create a parameters object to pass the name for the receipt rule to delete. To call the DeleteReceiptRuleCommand method, invoke an Amazon SES service object, passing the parameters.

Note

Replace RULE_NAME with the name of the rule, and RULE_SET_NAME with the rule set name.

import { DeleteReceiptRuleCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "../../libs/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const RULE_NAME = getUniqueName("RuleName"); const RULE_SET_NAME = getUniqueName("RuleSetName"); const createDeleteReceiptRuleCommand = () => { return new DeleteReceiptRuleCommand({ RuleName: RULE_NAME, RuleSetName: RULE_SET_NAME, }); }; const run = async () => { const deleteReceiptRuleCommand = createDeleteReceiptRuleCommand(); try { return await sesClient.send(deleteReceiptRuleCommand); } catch (err) { console.log("Failed to delete receipt rule.", err); return err; } };

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

node ses_deletereceiptrule.js

This example 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, including installing the required clients and packages.

Create a parameters object to pass the name for the new receipt rule set. To call the CreateReceiptRuleSetCommand method, invoke an Amazon SES client service object, passing the parameters.

Note

Replace REGION with your Amazon Region, RULE_SET_NAME with the rule set name.

import { CreateReceiptRuleSetCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "../../libs/utils/util-string.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const createCreateReceiptRuleSetCommand = (ruleSetName) => { return new CreateReceiptRuleSetCommand({ RuleSetName: ruleSetName }); }; const run = async () => { const createReceiptRuleSetCommand = createCreateReceiptRuleSetCommand(RULE_SET_NAME); try { return await sesClient.send(createReceiptRuleSetCommand); } catch (err) { console.log("Failed to create receipt rule set", err); return err; } };

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

node ses_createreceiptruleset.js

This example 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, including installing the required clients and packages.

Create an object to pass the name for the receipt rule set to delete. To call the DeleteReceiptRuleSetCommand method, invoke an Amazon SES client service object, passing the parameters.

Note

Replace RULE_SET_NAME with the rule set name.

import { DeleteReceiptRuleSetCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "../../libs/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const RULE_SET_NAME = getUniqueName("RuleSetName"); const createDeleteReceiptRuleSetCommand = () => { return new DeleteReceiptRuleSetCommand({ RuleSetName: RULE_SET_NAME }); }; const run = async () => { const deleteReceiptRuleSetCommand = createDeleteReceiptRuleSetCommand(); try { return await sesClient.send(deleteReceiptRuleSetCommand); } catch (err) { console.log("Failed to delete receipt rule set.", err); return err; } };

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

node ses_deletereceiptruleset.js // If you prefer JavaScript, enter 'node ses_deletereceiptruleset.js'

This example code can be found here on GitHub.