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:
-
Create a receipt rule set using CreateReceiptRuleSet.
-
Create a receipt rule using CreateReceiptRule.
-
Describe a receipt rule set using DescribeReceiptRuleSet.
-
Describe a receipt rule using DescribeReceiptRule.
-
List all receipt rule sets using ListReceiptRuleSets.
-
Update a receipt rule using UpdateReceiptRule.
-
Remove a receipt rule using DeleteReceiptRule.
-
Remove a receipt rule set using DeleteReceiptRuleSet.
All the example code for the AWS SDK for PHP is available here on
GitHub
Credentials
Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS SDK for PHP, as described in Basic usage.
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\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\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\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\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\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\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\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\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"; }