Creating and managing email rules with the SES API#

In addition to sending emails, you can also receive email with Amazon Simple Email Service (SES). Receipt rules enable you to specify what SES does with email it receives for the email addresses or domains you own. A rule can send email to other AWS services including but not limited to Amazon S3, Amazon SNS, or AWS Lambda.

For more information, see Managing Receipt Rule Sets for Amazon SES Email Receiving and Managing Receipt Rules for Amazon SES Email Receiving.

The following examples show how to:

Prerequisite tasks#

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

  • Configure your AWS credentials, as described in Quickstart.

Create a receipt rule set#

A receipt rule set contains a collection of receipt rules. You must have at least one receipt rule set associated with your account before you can create a receipt rule. To create a receipt rule set, provide a unique RuleSetName and use the CreateReceiptRuleSet operation.

Example#

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.create_receipt_rule_set(
  RuleSetName = 'RULE_SET_NAME',
)

print(response)

Create a receipt rule#

Control your incoming email by adding a receipt rule to an existing receipt rule set. This example shows you how to create a receipt rule that sends incoming messages to an Amazon S3 bucket, but you can also send messages to Amazon SNS and AWS Lambda. To create a receipt rule, provide a rule and the RuleSetName to the CreateReceiptRule operation.

Example#

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.create_receipt_rule(
  RuleSetName   = 'RULE_SET_NAME',
  Rule          = {
    'Name'      : 'RULE_NAME',
    'Enabled'   : True,
    'TlsPolicy' : 'Optional',
    'Recipients': [
      'EMAIL_ADDRESS',
    ],
    'Actions'   : [
      {
        'S3Action'         : {
          'BucketName'     : 'amzn-s3-demo-bucket',
          'ObjectKeyPrefix': 'SES_email'
        }
      }
    ],
  }
)

print(response)

Delete a receipt rule set#

Remove a specified receipt rule set that isn’t currently disabled. This also deletes all of the receipt rules it contains. To delete a receipt rule set, provide the RuleSetName to the DeleteReceiptRuleSet operation.

Example#

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.delete_receipt_rule(
  RuleName='RULE_NAME',
  RuleSetName='RULE_SET_NAME'
)

print(response)

Delete a receipt rule#

To delete a specified receipt rule, provide the RuleName and RuleSetName to the DeleteReceiptRule operation.

Example#

import boto3

# Create SES client
ses = boto3.client('ses')

response = ses.delete_receipt_rule_set(
  RuleSetName = 'RULE_SET_NAME'
)

print(response)