Use CreateReceiptRule
with an AWS SDK
The following code examples show how to use CreateReceiptRule
.
- C++
-
- SDK for C++
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. //! Create an Amazon Simple Email Service (Amazon SES) receipt rule. /*! \param receiptRuleName: The name for the receipt rule. \param s3BucketName: The name of the S3 bucket for incoming mail. \param s3ObjectKeyPrefix: The prefix for the objects in the S3 bucket. \param ruleSetName: The name of the rule set where the receipt rule is added. \param recipients: Aws::Vector of recipients. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createReceiptRule(const Aws::String &receiptRuleName, const Aws::String &s3BucketName, const Aws::String &s3ObjectKeyPrefix, const Aws::String &ruleSetName, const Aws::Vector<Aws::String> &recipients, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateReceiptRuleRequest createReceiptRuleRequest; Aws::SES::Model::S3Action s3Action; s3Action.SetBucketName(s3BucketName); s3Action.SetObjectKeyPrefix(s3ObjectKeyPrefix); Aws::SES::Model::ReceiptAction receiptAction; receiptAction.SetS3Action(s3Action); Aws::SES::Model::ReceiptRule receiptRule; receiptRule.SetName(receiptRuleName); receiptRule.WithRecipients(recipients); Aws::Vector<Aws::SES::Model::ReceiptAction> receiptActionList; receiptActionList.emplace_back(receiptAction); receiptRule.SetActions(receiptActionList); createReceiptRuleRequest.SetRuleSetName(ruleSetName); createReceiptRuleRequest.SetRule(receiptRule); auto outcome = sesClient.CreateReceiptRule(createReceiptRuleRequest); if (outcome.IsSuccess()) { std::cout << "Successfully created receipt rule." << std::endl; } else { std::cerr << "Error creating receipt rule. " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
-
For API details, see CreateReceiptRule in AWS SDK for C++ API Reference.
-
- JavaScript
-
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import { CreateReceiptRuleCommand, TlsPolicy } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/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); throw err; } };
-
For API details, see CreateReceiptRule in AWS SDK for JavaScript API Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create an Amazon S3 bucket where Amazon SES can put copies of incoming emails and create a rule that copies incoming email to the bucket for a specific list of recipients.
class SesReceiptHandler: """Encapsulates Amazon SES receipt handling functions.""" def __init__(self, ses_client, s3_resource): """ :param ses_client: A Boto3 Amazon SES client. :param s3_resource: A Boto3 Amazon S3 resource. """ self.ses_client = ses_client self.s3_resource = s3_resource def create_bucket_for_copy(self, bucket_name): """ Creates a bucket that can receive copies of emails from Amazon SES. This includes adding a policy to the bucket that grants Amazon SES permission to put objects in the bucket. :param bucket_name: The name of the bucket to create. :return: The newly created bucket. """ allow_ses_put_policy = { "Version": "2012-10-17", "Statement": [ { "Sid": "AllowSESPut", "Effect": "Allow", "Principal": {"Service": "ses.amazonaws.com"}, "Action": "s3:PutObject", "Resource": f"arn:aws:s3:::{bucket_name}/*", } ], } bucket = None try: bucket = self.s3_resource.create_bucket( Bucket=bucket_name, CreateBucketConfiguration={ "LocationConstraint": self.s3_resource.meta.client.meta.region_name }, ) bucket.wait_until_exists() bucket.Policy().put(Policy=json.dumps(allow_ses_put_policy)) logger.info("Created bucket %s to receive copies of emails.", bucket_name) except ClientError: logger.exception("Couldn't create bucket to receive copies of emails.") if bucket is not None: bucket.delete() raise else: return bucket def create_s3_copy_rule( self, rule_set_name, rule_name, recipients, bucket_name, prefix ): """ Creates a rule so that all emails received by the specified recipients are copied to an Amazon S3 bucket. :param rule_set_name: The name of a previously created rule set to contain this rule. :param rule_name: The name to give the rule. :param recipients: When an email is received by one of these recipients, it is copied to the Amazon S3 bucket. :param bucket_name: The name of the bucket to receive email copies. This bucket must allow Amazon SES to put objects into it. :param prefix: An object key prefix to give the emails copied to the bucket. """ try: self.ses_client.create_receipt_rule( RuleSetName=rule_set_name, Rule={ "Name": rule_name, "Enabled": True, "Recipients": recipients, "Actions": [ { "S3Action": { "BucketName": bucket_name, "ObjectKeyPrefix": prefix, } } ], }, ) logger.info( "Created rule %s to copy mail received by %s to bucket %s.", rule_name, recipients, bucket_name, ) except ClientError: logger.exception("Couldn't create rule %s.", rule_name) raise
-
For API details, see CreateReceiptRule in AWS SDK for Python (Boto3) API Reference.
-
For a complete list of AWS SDK developer guides and code examples, see Using Amazon SES with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.