AWS SDK を使用して Amazon SES 受信フィルターを作成する - AWS SDK コードサンプル

Doc AWS SDK Examples リポジトリには、他にも SDK の例があります。 AWS GitHub

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

AWS SDK を使用して Amazon SES 受信フィルターを作成する

次のコード例は、IP アドレスまたは IP アドレス範囲からの受信メールをブロックする Amazon SES 受信フィルターを作成する方法を示しています。

C++
SDK for C++
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

//! 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(); }
  • API の詳細については、「 API リファレンスCreateReceiptFilter」の「」を参照してください。 AWS SDK for C++

JavaScript
SDK for JavaScript (v3)
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

import { CreateReceiptFilterCommand, ReceiptFilterPolicy, } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-sdk-examples/libs/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 (err) { console.log("Failed to create filter.", err); return err; } };
  • API の詳細については、「 API リファレンスCreateReceiptFilter」の「」を参照してください。 AWS SDK for JavaScript

Python
SDK for Python (Boto3)
注記

については、こちらを参照してください GitHub。用例一覧を検索し、AWS コードサンプルリポジトリでの設定と実行の方法を確認してください。

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
  • API の詳細については、CreateReceiptFilterAWS「 SDK for Python (Boto3) API リファレンス」の「」を参照してください。