AWS SDK for PHP
Developer Guide

Creating and Managing Email Rules Using the Amazon SES API and the AWS SDK for PHP Version 3

In addition to sending emails, you can also receive email with Amazon Simple Email Service (Amazon SES). Receipt rules enable you to specify what Amazon 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:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

For more information about using Amazon SES, see the Amazon SES Developer Guide.

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.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Rule_Set_Name'; try { $result = $SesClient->createReceiptRuleSet([ 'RuleSetName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

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.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $rule_name = 'Rule_Name'; $rule_set_name = 'Rule_Set_Name'; $s3_bucket = 'Bucket_Name'; try { $result = $SesClient->createReceiptRule([ 'Rule' => [ 'Actions' => [ [ 'S3Action' => [ 'BucketName' => $s3_bucket, ], ], ], 'Name' => $rule_name, 'ScanEnabled' => true, 'TlsPolicy' => 'Optional', 'Recipients' => ['<string>', ...] ], 'RuleSetName' => $rule_set_name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Describe a Receipt Rule Set

Once per second, return the details of the specified receipt rule set. To use the DescribeReceiptRuleSet operation, provide the RuleSetName.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Rule_Set_Name'; try { $result = $SesClient->describeReceiptRuleSet([ 'RuleSetName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Describe a Receipt Rule

Return the details of a specified receipt rule. To use the DescribeReceiptRule operation, provide the RuleName and RuleSetName.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $rule_name = 'Rule_Name'; $rule_set_name = 'Rule_Set_Name'; try { $result = $SesClient->describeReceiptRule([ 'RuleName' => $rule_name, 'RuleSetName' => $rule_set_name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

List All Receipt Rule Sets

To list the receipt rule sets that exist under your AWS account in the current AWS Region, use the ListReceiptRuleSets operation.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); try { $result = $SesClient->listReceiptRuleSets([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Update a Receipt Rule

This example shows you how to update a receipt rule that sends incoming messages to an AWS Lambda function, but you can also send messages to Amazon SNS and Amazon S3. To use the UpdateReceiptRule operation, provide the new receipt rule and the RuleSetName.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $rule_name = 'Rule_Name'; $rule_set_name = 'Rule_Set_Name'; $lambda_arn = 'Amazon Resource Name (ARN) of the AWS Lambda function'; $sns_topic_arn = 'Amazon Resource Name (ARN) of the Amazon SNS topic'; try { $result = $SesClient->updateReceiptRule([ 'Rule' => [ 'Actions' => [ 'LambdaAction' => [ 'FunctionArn' => $lambda_arn, 'TopicArn' => $sns_topic_arn, ], ], 'Enabled' => true, 'Name' => $rule_name, 'ScanEnabled' => false, 'TlsPolicy' => 'Require', ], 'RuleSetName' => $rule_set_name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

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.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Rule_Set_Name'; try { $result = $SesClient->deleteReceiptRuleSet([ 'RuleSetName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Delete a Receipt Rule

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

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $rule_name = 'Rule_Name'; $rule_set_name = 'Rule_Set_Name'; try { $result = $SesClient->deleteReceiptRule([ 'RuleName' => $rule_name, 'RuleSetName' => $rule_set_name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }