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

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります

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

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

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

JavaScript
SDK forJavaScript v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

import { CreateReceiptFilterCommand, ReceiptFilterPolicy, } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "../../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 の詳細については、AWS SDK for JavaScriptAPI CreateReceiptFilterリファレンスのを参照してください

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