Use CreateReceiptFilter with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use CreateReceiptFilter with an AWS SDK or CLI

The following code examples show how to use CreateReceiptFilter.

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 filter.. /*! \param receiptFilterName: The name for the receipt filter. \param cidr: IP address or IP address range in Classless Inter-Domain Routing (CIDR) notation. \param policy: Block or allow enum of type ReceiptFilterPolicy. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::SES::createReceiptFilter(const Aws::String &receiptFilterName, const Aws::String &cidr, Aws::SES::Model::ReceiptFilterPolicy policy, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::SES::SESClient sesClient(clientConfiguration); Aws::SES::Model::CreateReceiptFilterRequest createReceiptFilterRequest; Aws::SES::Model::ReceiptFilter receiptFilter; Aws::SES::Model::ReceiptIpFilter receiptIpFilter; receiptIpFilter.SetCidr(cidr); receiptIpFilter.SetPolicy(policy); receiptFilter.SetName(receiptFilterName); receiptFilter.SetIpFilter(receiptIpFilter); createReceiptFilterRequest.SetFilter(receiptFilter); Aws::SES::Model::CreateReceiptFilterOutcome createReceiptFilterOutcome = sesClient.CreateReceiptFilter( createReceiptFilterRequest); if (createReceiptFilterOutcome.IsSuccess()) { std::cout << "Successfully created receipt filter." << std::endl; } else { std::cerr << "Error creating receipt filter: " << createReceiptFilterOutcome.GetError().GetMessage() << std::endl; } return createReceiptFilterOutcome.IsSuccess(); }
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 { CreateReceiptFilterCommand, ReceiptFilterPolicy, } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const createCreateReceiptFilterCommand = ({ policy, ipOrRange, name }) => { return new CreateReceiptFilterCommand({ Filter: { IpFilter: { Cidr: ipOrRange, // string, either a single IP address (10.0.0.1) or an IP address range in CIDR notation (10.0.0.1/24)). Policy: policy, // enum ReceiptFilterPolicy, email traffic from the filtered addressesOptions. }, /* The name of the IP address filter. Only ASCII letters, numbers, underscores, or dashes. Must be less than 64 characters and start and end with a letter or number. */ Name: name, }, }); }; const FILTER_NAME = getUniqueName("ReceiptFilter"); const run = async () => { const createReceiptFilterCommand = createCreateReceiptFilterCommand({ policy: ReceiptFilterPolicy.Allow, ipOrRange: "10.0.0.1", name: FILTER_NAME, }); try { return await sesClient.send(createReceiptFilterCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };
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.

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_receipt_filter(self, filter_name, ip_address_or_range, allow): """ Creates a filter that allows or blocks incoming mail from an IP address or range. :param filter_name: The name to give the filter. :param ip_address_or_range: The IP address or range to block or allow. :param allow: When True, incoming mail is allowed from the specified IP address or range; otherwise, it is blocked. """ try: policy = "Allow" if allow else "Block" self.ses_client.create_receipt_filter( Filter={ "Name": filter_name, "IpFilter": {"Cidr": ip_address_or_range, "Policy": policy}, } ) logger.info( "Created receipt filter %s to %s IP of %s.", filter_name, policy, ip_address_or_range, ) except ClientError: logger.exception("Couldn't create receipt filter %s.", filter_name) raise