搭CreateReceiptRule配 AWS 開發套件或 CLI 使用 - Amazon Simple Email Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

CreateReceiptRule配 AWS 開發套件或 CLI 使用

下列程式碼範例會示範如何使用CreateReceiptRule

C++
適用於 C++ 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

//! 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(); }
  • 如需 API 詳細資訊,請參閱 AWS SDK for C++ API 參考CreateReceiptRule中的。

JavaScript
適用於 JavaScript (v3) 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

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; } };
  • 如需 API 詳細資訊,請參閱 AWS SDK for JavaScript API 參考CreateReceiptRule中的。

Python
適用於 Python (Boto3) 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

建立 Amazon S3 儲存貯體,在此儲存貯體中 Amazon SES 可以放置傳入電子郵件的副本,並建立規則將傳入電子郵件複製到特定收件人列表的儲存貯體。

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
  • 如需 API 的詳細資訊,請參閱AWS 開發套件CreateReceiptRule中的 Python (博托 3) API 參考。

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS 開發套件使用 Amazon SES。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。