Use CreateReceiptRule com um AWS SDK ou CLI - Amazon Simple Email Service

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Use CreateReceiptRule com um AWS SDK ou CLI

Os exemplos de códigos a seguir mostram como usar CreateReceiptRule.

C++
SDK para C++
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no 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(); }
  • Para obter detalhes da API, consulte CreateReceiptRulea Referência AWS SDK for C++ da API.

JavaScript
SDK para JavaScript (v3)
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no 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; } };
  • Para obter detalhes da API, consulte CreateReceiptRulea Referência AWS SDK for JavaScript da API.

Python
SDK para Python (Boto3).
nota

Tem mais sobre GitHub. Encontre o exemplo completo e veja como configurar e executar no AWS Code Examples Repository.

Crie um bucket do Amazon S3 no qual o Amazon SES possa colocar cópias de e-mails recebidos e crie uma regra que copia para o bucket os e-mails recebidos de uma lista específica de destinatários.

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
  • Para obter detalhes da API, consulte a CreateReceiptRuleReferência da API AWS SDK for Python (Boto3).

Para obter uma lista completa dos guias do desenvolvedor do AWS SDK e exemplos de código, consulteUsando o Amazon SES com um AWS SDK. Este tópico também inclui informações sobre como começar e detalhes sobre versões anteriores do SDK.