Python(Boto3)SDK용 Amazon SES 예제 - AWS SDK 코드 예제

AWS 문서 예제 리포지토리에서 더 많은 SDK GitHub AWS SDK 예제를 사용할 수 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Python(Boto3)SDK용 Amazon SES 예제

다음 코드 예제에서는 Amazon 에서 를 사용하여 작업을 수행하고 일반적인 시나리오 AWS SDK for Python (Boto3) 를 구현하는 방법을 보여줍니다SES.

작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.

시나리오는 동일한 서비스 내에서 또는 다른 AWS 서비스와 결합된 상태에서 여러 함수를 호출하여 특정 태스크를 수행하는 방법을 보여주는 코드 예제입니다.

각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.

작업

다음 코드 예시에서는 CreateReceiptFilter을 사용하는 방법을 보여 줍니다.

SDK 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 Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 CreateReceiptRule을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Amazon S3 버킷을 생성SES하여 Amazon이 수신 이메일의 사본을 넣고 수신 이메일을 특정 수신자 목록에 대해 버킷에 복사하는 규칙을 생성합니다.

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 자세한 내용은 CreateReceiptRuleAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 CreateReceiptRuleSet을 사용하는 방법을 보여 줍니다.

SDK 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_rule_set(self, rule_set_name): """ Creates an empty rule set. Rule sets contain individual rules and can be used to organize rules. :param rule_set_name: The name to give the rule set. """ try: self.ses_client.create_receipt_rule_set(RuleSetName=rule_set_name) logger.info("Created receipt rule set %s.", rule_set_name) except ClientError: logger.exception("Couldn't create receipt rule set %s.", rule_set_name) raise
  • API 자세한 내용은 CreateReceiptRuleSetAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 CreateTemplate을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def create_template(self, name, subject, text, html): """ Creates an email template. :param name: The name of the template. :param subject: The subject of the email. :param text: The plain text version of the email. :param html: The HTML version of the email. """ try: template = { "TemplateName": name, "SubjectPart": subject, "TextPart": text, "HtmlPart": html, } self.ses_client.create_template(Template=template) logger.info("Created template %s.", name) self.template = template self._extract_tags(subject, text, html) except ClientError: logger.exception("Couldn't create template %s.", name) raise
  • API 자세한 내용은 CreateTemplateAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DeleteIdentity을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def delete_identity(self, identity): """ Deletes an identity. :param identity: The identity to remove. """ try: self.ses_client.delete_identity(Identity=identity) logger.info("Deleted identity %s.", identity) except ClientError: logger.exception("Couldn't delete identity %s.", identity) raise
  • API 자세한 내용은 DeleteIdentityAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DeleteReceiptFilter을 사용하는 방법을 보여 줍니다.

SDK 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 delete_receipt_filter(self, filter_name): """ Deletes a receipt filter. :param filter_name: The name of the filter to delete. """ try: self.ses_client.delete_receipt_filter(FilterName=filter_name) logger.info("Deleted receipt filter %s.", filter_name) except ClientError: logger.exception("Couldn't delete receipt filter %s.", filter_name) raise
  • API 자세한 내용은 DeleteReceiptFilterAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DeleteReceiptRule을 사용하는 방법을 보여 줍니다.

SDK 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 delete_receipt_rule(self, rule_set_name, rule_name): """ Deletes a rule. :param rule_set_name: The rule set that contains the rule to delete. :param rule_name: The rule to delete. """ try: self.ses_client.delete_receipt_rule( RuleSetName=rule_set_name, RuleName=rule_name ) logger.info("Removed rule %s from rule set %s.", rule_name, rule_set_name) except ClientError: logger.exception( "Couldn't remove rule %s from rule set %s.", rule_name, rule_set_name ) raise
  • API 자세한 내용은 DeleteReceiptRuleAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DeleteReceiptRuleSet을 사용하는 방법을 보여 줍니다.

SDK 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 delete_receipt_rule_set(self, rule_set_name): """ Deletes a rule set. When a rule set is deleted, all of the rules it contains are also deleted. :param rule_set_name: The name of the rule set to delete. """ try: self.ses_client.delete_receipt_rule_set(RuleSetName=rule_set_name) logger.info("Deleted rule set %s.", rule_set_name) except ClientError: logger.exception("Couldn't delete rule set %s.", rule_set_name) raise
  • API 자세한 내용은 DeleteReceiptRuleSetAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DeleteTemplate을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def delete_template(self): """ Deletes an email template. """ try: self.ses_client.delete_template(TemplateName=self.template["TemplateName"]) logger.info("Deleted template %s.", self.template["TemplateName"]) self.template = None self.template_tags = None except ClientError: logger.exception( "Couldn't delete template %s.", self.template["TemplateName"] ) raise
  • API 자세한 내용은 DeleteTemplateAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 DescribeReceiptRuleSet을 사용하는 방법을 보여 줍니다.

SDK 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 describe_receipt_rule_set(self, rule_set_name): """ Gets data about a rule set. :param rule_set_name: The name of the rule set to retrieve. :return: Data about the rule set. """ try: response = self.ses_client.describe_receipt_rule_set( RuleSetName=rule_set_name ) logger.info("Got data for rule set %s.", rule_set_name) except ClientError: logger.exception("Couldn't get data for rule set %s.", rule_set_name) raise else: return response
  • API 자세한 내용은 DescribeReceiptRuleSetAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 GetIdentityVerificationAttributes을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def get_identity_status(self, identity): """ Gets the status of an identity. This can be used to discover whether an identity has been successfully verified. :param identity: The identity to query. :return: The status of the identity. """ try: response = self.ses_client.get_identity_verification_attributes( Identities=[identity] ) status = response["VerificationAttributes"].get( identity, {"VerificationStatus": "NotFound"} )["VerificationStatus"] logger.info("Got status of %s for %s.", status, identity) except ClientError: logger.exception("Couldn't get status for %s.", identity) raise else: return status

다음 코드 예시에서는 GetTemplate을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def get_template(self, name): """ Gets a previously created email template. :param name: The name of the template to retrieve. :return: The retrieved email template. """ try: response = self.ses_client.get_template(TemplateName=name) self.template = response["Template"] logger.info("Got template %s.", name) self._extract_tags( self.template["SubjectPart"], self.template["TextPart"], self.template["HtmlPart"], ) except ClientError: logger.exception("Couldn't get template %s.", name) raise else: return self.template
  • API 자세한 내용은 GetTemplateAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 ListIdentities을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def list_identities(self, identity_type, max_items): """ Gets the identities of the specified type for the current account. :param identity_type: The type of identity to retrieve, such as EmailAddress. :param max_items: The maximum number of identities to retrieve. :return: The list of retrieved identities. """ try: response = self.ses_client.list_identities( IdentityType=identity_type, MaxItems=max_items ) identities = response["Identities"] logger.info("Got %s identities for the current account.", len(identities)) except ClientError: logger.exception("Couldn't list identities for the current account.") raise else: return identities
  • API 자세한 내용은 ListIdentitiesAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 ListReceiptFilters을 사용하는 방법을 보여 줍니다.

SDK 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 list_receipt_filters(self): """ Gets the list of receipt filters for the current account. :return: The list of receipt filters. """ try: response = self.ses_client.list_receipt_filters() filters = response["Filters"] logger.info("Got %s receipt filters.", len(filters)) except ClientError: logger.exception("Couldn't get receipt filters.") raise else: return filters
  • API 자세한 내용은 ListReceiptFiltersAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 ListTemplates을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def list_templates(self): """ Gets a list of all email templates for the current account. :return: The list of retrieved email templates. """ try: response = self.ses_client.list_templates() templates = response["TemplatesMetadata"] logger.info("Got %s templates.", len(templates)) except ClientError: logger.exception("Couldn't get templates.") raise else: return templates
  • API 자세한 내용은 ListTemplatesAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 SendEmail을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesMailSender: """Encapsulates functions to send emails with Amazon SES.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def send_email(self, source, destination, subject, text, html, reply_tos=None): """ Sends an email. Note: If your account is in the Amazon SES sandbox, the source and destination email accounts must both be verified. :param source: The source email account. :param destination: The destination email account. :param subject: The subject of the email. :param text: The plain text version of the body of the email. :param html: The HTML version of the body of the email. :param reply_tos: Email accounts that will receive a reply if the recipient replies to the message. :return: The ID of the message, assigned by Amazon SES. """ send_args = { "Source": source, "Destination": destination.to_service_format(), "Message": { "Subject": {"Data": subject}, "Body": {"Text": {"Data": text}, "Html": {"Data": html}}, }, } if reply_tos is not None: send_args["ReplyToAddresses"] = reply_tos try: response = self.ses_client.send_email(**send_args) message_id = response["MessageId"] logger.info( "Sent mail %s from %s to %s.", message_id, source, destination.tos ) except ClientError: logger.exception( "Couldn't send mail from %s to %s.", source, destination.tos ) raise else: return message_id
  • API 자세한 내용은 SendEmailAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 SendTemplatedEmail을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesMailSender: """Encapsulates functions to send emails with Amazon SES.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def send_templated_email( self, source, destination, template_name, template_data, reply_tos=None ): """ Sends an email based on a template. A template contains replaceable tags each enclosed in two curly braces, such as {{name}}. The template data passed in this function contains key-value pairs that define the values to insert in place of the template tags. Note: If your account is in the Amazon SES sandbox, the source and destination email accounts must both be verified. :param source: The source email account. :param destination: The destination email account. :param template_name: The name of a previously created template. :param template_data: JSON-formatted key-value pairs of replacement values that are inserted in the template before it is sent. :return: The ID of the message, assigned by Amazon SES. """ send_args = { "Source": source, "Destination": destination.to_service_format(), "Template": template_name, "TemplateData": json.dumps(template_data), } if reply_tos is not None: send_args["ReplyToAddresses"] = reply_tos try: response = self.ses_client.send_templated_email(**send_args) message_id = response["MessageId"] logger.info( "Sent templated mail %s from %s to %s.", message_id, source, destination.tos, ) except ClientError: logger.exception( "Couldn't send templated mail from %s to %s.", source, destination.tos ) raise else: return message_id
  • API 자세한 내용은 Python(Boto3) 참조 SendTemplatedEmail 의 섹션을 참조하세요. AWS SDK API

다음 코드 예시에서는 UpdateTemplate을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def update_template(self, name, subject, text, html): """ Updates a previously created email template. :param name: The name of the template. :param subject: The subject of the email. :param text: The plain text version of the email. :param html: The HTML version of the email. """ try: template = { "TemplateName": name, "SubjectPart": subject, "TextPart": text, "HtmlPart": html, } self.ses_client.update_template(Template=template) logger.info("Updated template %s.", name) self.template = template self._extract_tags(subject, text, html) except ClientError: logger.exception("Couldn't update template %s.", name) raise
  • API 자세한 내용은 UpdateTemplateAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 VerifyDomainIdentity을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def verify_domain_identity(self, domain_name): """ Starts verification of a domain identity. To complete verification, you must create a TXT record with a specific format through your DNS provider. For more information, see *Verifying a domain with Amazon SES* in the Amazon SES documentation: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-domain-procedure.html :param domain_name: The name of the domain to verify. :return: The token to include in the TXT record with your DNS provider. """ try: response = self.ses_client.verify_domain_identity(Domain=domain_name) token = response["VerificationToken"] logger.info("Got domain verification token for %s.", domain_name) except ClientError: logger.exception("Couldn't verify domain %s.", domain_name) raise else: return token
  • API 자세한 내용은 VerifyDomainIdentityAWS SDK Python(Boto3) API 참조 섹션을 참조하세요.

다음 코드 예시에서는 VerifyEmailIdentity을 사용하는 방법을 보여 줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def verify_email_identity(self, email_address): """ Starts verification of an email identity. This function causes an email to be sent to the specified email address from Amazon SES. To complete verification, follow the instructions in the email. :param email_address: The email address to verify. """ try: self.ses_client.verify_email_identity(EmailAddress=email_address) logger.info("Started verification of %s.", email_address) except ClientError: logger.exception("Couldn't start verification of %s.", email_address) raise
  • API 자세한 내용은 Python(Boto3) 참조 VerifyEmailIdentity 의 섹션을 참조하세요. AWS SDK API

시나리오

다음 코드 예제에서는 Amazon SES 이메일 및 도메인 ID를 한 AWS 리전에서 다른 리전으로 복사하는 방법을 보여줍니다. Route 53에서 도메인 자격 증명을 관리하는 경우 확인 레코드가 대상 리전의 도메인에 복사됩니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

import argparse import json import logging from pprint import pprint import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def get_identities(ses_client): """ Gets the identities for the current Region. The Region is specified in the Boto3 Amazon SES client object. :param ses_client: A Boto3 Amazon SES client. :return: The list of email identities and the list of domain identities. """ email_identities = [] domain_identities = [] try: identity_paginator = ses_client.get_paginator("list_identities") identity_iterator = identity_paginator.paginate( PaginationConfig={"PageSize": 20} ) for identity_page in identity_iterator: for identity in identity_page["Identities"]: if "@" in identity: email_identities.append(identity) else: domain_identities.append(identity) logger.info( "Found %s email and %s domain identities.", len(email_identities), len(domain_identities), ) except ClientError: logger.exception("Couldn't get identities.") raise else: return email_identities, domain_identities def verify_emails(email_list, ses_client): """ Starts verification of a list of email addresses. Verification causes an email to be sent to each address. To complete verification, the recipient must follow the instructions in the email. :param email_list: The list of email addresses to verify. :param ses_client: A Boto3 Amazon SES client. :return: The list of emails that were successfully submitted for verification. """ verified_emails = [] for email in email_list: try: ses_client.verify_email_identity(EmailAddress=email) verified_emails.append(email) logger.info("Started verification of %s.", email) except ClientError: logger.warning("Couldn't start verification of %s.", email) return verified_emails def verify_domains(domain_list, ses_client): """ Starts verification for a list of domain identities. This returns a token for each domain, which must be registered as a TXT record with the DNS provider for the domain. :param domain_list: The list of domains to verify. :param ses_client: A Boto3 Amazon SES client. :return: The generated domain tokens to use to completed verification. """ domain_tokens = {} for domain in domain_list: try: response = ses_client.verify_domain_identity(Domain=domain) token = response["VerificationToken"] domain_tokens[domain] = token logger.info("Got verification token %s for domain %s.", token, domain) except ClientError: logger.warning("Couldn't get verification token for domain %s.", domain) return domain_tokens def get_hosted_zones(route53_client): """ Gets the Amazon Route 53 hosted zones for the current account. :param route53_client: A Boto3 Route 53 client. :return: The list of hosted zones. """ zones = [] try: zone_paginator = route53_client.get_paginator("list_hosted_zones") zone_iterator = zone_paginator.paginate(PaginationConfig={"PageSize": 20}) zones = [ zone for zone_page in zone_iterator for zone in zone_page["HostedZones"] ] logger.info("Found %s hosted zones.", len(zones)) except ClientError: logger.warning("Couldn't get hosted zones.") return zones def find_domain_zone_matches(domains, zones): """ Finds matches between Amazon SES verified domains and Route 53 hosted zones. Subdomain matches are taken when found, otherwise root domain matches are taken. :param domains: The list of domains to match. :param zones: The list of hosted zones to match. :return: The set of matched domain-zone pairs. When a match is not found, the domain is included in the set with a zone value of None. """ domain_zones = {} for domain in domains: domain_zones[domain] = None # Start at the most specific sub-domain and walk up to the root domain until a # zone match is found. domain_split = domain.split(".") for index in range(0, len(domain_split) - 1): sub_domain = ".".join(domain_split[index:]) for zone in zones: # Normalize the zone name from Route 53 by removing the trailing '.'. zone_name = zone["Name"][:-1] if sub_domain == zone_name: domain_zones[domain] = zone break if domain_zones[domain] is not None: break return domain_zones def add_route53_verification_record(domain, token, zone, route53_client): """ Adds a domain verification TXT record to the specified Route 53 hosted zone. When a TXT record already exists in the hosted zone for the specified domain, the existing values are preserved and the new token is added to the list. :param domain: The domain to add. :param token: The verification token for the domain. :param zone: The hosted zone where the domain verification record is added. :param route53_client: A Boto3 Route 53 client. """ domain_token_record_set_name = f"_amazonses.{domain}" record_set_paginator = route53_client.get_paginator("list_resource_record_sets") record_set_iterator = record_set_paginator.paginate( HostedZoneId=zone["Id"], PaginationConfig={"PageSize": 20} ) records = [] for record_set_page in record_set_iterator: try: txt_record_set = next( record_set for record_set in record_set_page["ResourceRecordSets"] if record_set["Name"][:-1] == domain_token_record_set_name and record_set["Type"] == "TXT" ) records = txt_record_set["ResourceRecords"] logger.info( "Existing TXT record found in set %s for zone %s.", domain_token_record_set_name, zone["Name"], ) break except StopIteration: pass records.append({"Value": json.dumps(token)}) changes = [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": domain_token_record_set_name, "Type": "TXT", "TTL": 1800, "ResourceRecords": records, }, } ] try: route53_client.change_resource_record_sets( HostedZoneId=zone["Id"], ChangeBatch={"Changes": changes} ) logger.info( "Created or updated the TXT record in set %s for zone %s.", domain_token_record_set_name, zone["Name"], ) except ClientError as err: logger.warning( "Got error %s. Couldn't create or update the TXT record for zone %s.", err.response["Error"]["Code"], zone["Name"], ) def generate_dkim_tokens(domain, ses_client): """ Generates DKIM tokens for a domain. These must be added as CNAME records to the DNS provider for the domain. :param domain: The domain to generate tokens for. :param ses_client: A Boto3 Amazon SES client. :return: The list of generated DKIM tokens. """ dkim_tokens = [] try: dkim_tokens = ses_client.verify_domain_dkim(Domain=domain)["DkimTokens"] logger.info("Generated %s DKIM tokens for domain %s.", len(dkim_tokens), domain) except ClientError: logger.warning("Couldn't generate DKIM tokens for domain %s.", domain) return dkim_tokens def add_dkim_domain_tokens(hosted_zone, domain, tokens, route53_client): """ Adds DKIM domain token CNAME records to a Route 53 hosted zone. :param hosted_zone: The hosted zone where the records are added. :param domain: The domain to add. :param tokens: The DKIM tokens for the domain to add. :param route53_client: A Boto3 Route 53 client. """ try: changes = [ { "Action": "UPSERT", "ResourceRecordSet": { "Name": f"{token}._domainkey.{domain}", "Type": "CNAME", "TTL": 1800, "ResourceRecords": [{"Value": f"{token}.dkim.amazonses.com"}], }, } for token in tokens ] route53_client.change_resource_record_sets( HostedZoneId=hosted_zone["Id"], ChangeBatch={"Changes": changes} ) logger.info( "Added %s DKIM CNAME records to %s in zone %s.", len(tokens), domain, hosted_zone["Name"], ) except ClientError: logger.warning( "Couldn't add DKIM CNAME records for %s to zone %s.", domain, hosted_zone["Name"], ) def configure_sns_topics(identity, topics, ses_client): """ Configures Amazon Simple Notification Service (Amazon SNS) notifications for an identity. The Amazon SNS topics must already exist. :param identity: The identity to configure. :param topics: The list of topics to configure. The choices are Bounce, Delivery, or Complaint. :param ses_client: A Boto3 Amazon SES client. """ for topic in topics: topic_arn = input( f"Enter the Amazon Resource Name (ARN) of the {topic} topic or press " f"Enter to skip: " ) if topic_arn != "": try: ses_client.set_identity_notification_topic( Identity=identity, NotificationType=topic, SnsTopic=topic_arn ) logger.info("Configured %s for %s notifications.", identity, topic) except ClientError: logger.warning( "Couldn't configure %s for %s notifications.", identity, topic ) def replicate(source_client, destination_client, route53_client): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print( f"Replicating Amazon SES identities and other configuration from " f"{source_client.meta.region_name} to {destination_client.meta.region_name}." ) print("-" * 88) print(f"Retrieving identities from {source_client.meta.region_name}.") source_emails, source_domains = get_identities(source_client) print("Email addresses found:") print(*source_emails) print("Domains found:") print(*source_domains) print("Starting verification for email identities.") dest_emails = verify_emails(source_emails, destination_client) print("Getting domain tokens for domain identities.") dest_domain_tokens = verify_domains(source_domains, destination_client) # Get Route 53 hosted zones and match them with Amazon SES domains. answer = input( "Is the DNS configuration for your domains managed by Amazon Route 53 (y/n)? " ) use_route53 = answer.lower() == "y" hosted_zones = get_hosted_zones(route53_client) if use_route53 else [] if use_route53: print("Adding or updating Route 53 TXT records for your domains.") domain_zones = find_domain_zone_matches(dest_domain_tokens.keys(), hosted_zones) for domain in domain_zones: add_route53_verification_record( domain, dest_domain_tokens[domain], domain_zones[domain], route53_client ) else: print( "Use these verification tokens to create TXT records through your DNS " "provider:" ) pprint(dest_domain_tokens) answer = input("Do you want to configure DKIM signing for your identities (y/n)? ") if answer.lower() == "y": # Build a set of unique domains from email and domain identities. domains = {email.split("@")[1] for email in dest_emails} domains.update(dest_domain_tokens) domain_zones = find_domain_zone_matches(domains, hosted_zones) for domain, zone in domain_zones.items(): answer = input( f"Do you want to configure DKIM signing for {domain} (y/n)? " ) if answer.lower() == "y": dkim_tokens = generate_dkim_tokens(domain, destination_client) if use_route53 and zone is not None: add_dkim_domain_tokens(zone, domain, dkim_tokens, route53_client) else: print( "Add the following DKIM tokens as CNAME records through your " "DNS provider:" ) print(*dkim_tokens, sep="\n") answer = input( "Do you want to configure Amazon SNS notifications for your identities (y/n)? " ) if answer.lower() == "y": for identity in dest_emails + list(dest_domain_tokens.keys()): answer = input( f"Do you want to configure Amazon SNS topics for {identity} (y/n)? " ) if answer.lower() == "y": configure_sns_topics( identity, ["Bounce", "Delivery", "Complaint"], destination_client ) print(f"Replication complete for {destination_client.meta.region_name}.") print("-" * 88) def main(): boto3_session = boto3.Session() ses_regions = boto3_session.get_available_regions("ses") parser = argparse.ArgumentParser( description="Copies email address and domain identities from one AWS Region to " "another. Optionally adds records for domain verification and DKIM " "signing to domains that are managed by Amazon Route 53, " "and sets up Amazon SNS notifications for events of interest." ) parser.add_argument( "source_region", choices=ses_regions, help="The region to copy from." ) parser.add_argument( "destination_region", choices=ses_regions, help="The region to copy to." ) args = parser.parse_args() source_client = boto3.client("ses", region_name=args.source_region) destination_client = boto3.client("ses", region_name=args.destination_region) route53_client = boto3.client("route53") replicate(source_client, destination_client, route53_client) if __name__ == "__main__": main()

다음 코드 예제는 Amazon DynamoDB 테이블의 작업 항목을 추적하고 Amazon Simple Email Service(AmazonSES)를 사용하여 보고서를 전송하는 웹 애플리케이션을 생성하는 방법을 보여줍니다.

SDK Python용(Boto3)

AWS SDK for Python (Boto3) 를 사용하여 Amazon DynamoDB의 작업 항목을 추적하고 Amazon Simple Email Service(Amazon)를 사용하여 보고서를 이메일로 보내는 REST 서비스를 생성하는 방법을 보여줍니다SES. 이 예제에서는 Flask 웹 프레임워크를 사용하여 HTTP 라우팅을 처리하고 React 웹 페이지와 통합하여 완전한 기능을 갖춘 웹 애플리케이션을 제공합니다.

  • 와 통합되는 Flask REST 서비스를 빌드합니다 AWS 서비스.

  • DynamoDB 테이블에 저장된 작업 항목을 읽고, 쓰고, 업데이트합니다.

  • AmazonSES을 사용하여 작업 항목에 대한 이메일 보고서를 보냅니다.

전체 소스 코드와 설정 및 실행 방법에 대한 지침은 의 AWS 코드 예제 리포지토리에서 전체 예제를 참조하세요 GitHub.

이 예제에서 사용되는 서비스
  • DynamoDB

  • Amazon SES

다음 코드 예제는 Amazon Aurora Serverless 데이터베이스의 작업 항목을 추적하고 Amazon Simple Email Service(AmazonSES)를 사용하여 보고서를 전송하는 웹 애플리케이션을 생성하는 방법을 보여줍니다.

SDK Python용(Boto3)

AWS SDK for Python (Boto3) 를 사용하여 Amazon Aurora Serverless 데이터베이스의 작업 항목을 추적하고 Amazon Simple Email Service(Amazon)를 사용하여 보고서를 이메일로 보내는 REST 서비스를 생성하는 방법을 보여줍니다SES. 이 예제에서는 Flask 웹 프레임워크를 사용하여 HTTP 라우팅을 처리하고 React 웹 페이지와 통합하여 완전한 기능을 갖춘 웹 애플리케이션을 제공합니다.

  • 와 통합되는 Flask REST 서비스를 빌드합니다 AWS 서비스.

  • Aurora Serverless 데이터베이스에 저장된 작업 항목을 읽고, 쓰고, 업데이트합니다.

  • 데이터베이스 보안 인증 정보가 포함된 AWS Secrets Manager 보안 암호를 생성하고 이를 사용하여 데이터베이스에 대한 호출을 인증합니다.

  • AmazonSES을 사용하여 작업 항목에 대한 이메일 보고서를 보냅니다.

전체 소스 코드와 설정 및 실행 방법에 대한 지침은 의 전체 예제를 참조하세요GitHub.

이 예시에서 사용되는 서비스
  • Aurora

  • Amazon RDS

  • Amazon RDS Data Service

  • Amazon SES

다음 코드 예제는 Amazon Rekognition을 사용하여 이미지의 범주별로 객체를 감지하는 앱을 구축하는 방법을 보여줍니다.

SDK Python용(Boto3)

를 사용하여 다음을 수행할 수 있는 웹 애플리케이션을 AWS SDK for Python (Boto3) 생성하는 방법을 보여줍니다.

  • 사진을 Amazon Simple Storage Service (Amazon S3) 버킷에 업로드합니다.

  • Amazon Rekognition을 사용하여 사진을 분석하고 레이블을 지정합니다.

  • Amazon Simple Email Service(AmazonSES)를 사용하여 이미지 분석에 대한 이메일 보고서를 전송합니다.

이 예제에는 React로 빌드 JavaScript 된 에 작성된 웹 페이지와 Flask-로 빌드된 Python에 작성된 REST 서비스라는 두 가지 주요 구성 요소가 포함되어 있습니다RESTful.

React 웹 페이지를 사용하여 다음을 수행할 수 있습니다.

  • S3 버킷에 저장된 이미지 목록을 표시합니다.

  • 컴퓨터에서 S3 버킷에 이미지를 업로드합니다.

  • 이미지에서 감지된 항목을 식별하는 이미지와 레이블을 표시합니다.

  • S3 버킷의 모든 이미지에 대한 보고서를 받고 보고서의 이메일을 보냅니다.

웹 페이지에서 REST 서비스를 호출합니다. 서비스가 다음 작업을 수행하기 위해 AWS 에 요청을 전송합니다.

  • S3 버킷의 이미지 목록을 가져오고 필터링합니다.

  • S3 버킷에 사진을 업로드합니다.

  • Amazon Rekognition을 사용하여 개별 사진을 분석하고 사진에서 감지된 항목을 식별하는 레이블 목록을 가져옵니다.

  • S3 버킷의 모든 사진을 분석하고 AmazonSES을 사용하여 보고서를 이메일로 보냅니다.

전체 소스 코드와 설정 및 실행 방법에 대한 지침은 의 전체 예제를 참조하세요GitHub.

이 예시에서 사용되는 서비스
  • Amazon Rekognition

  • Amazon S3

  • Amazon SES

다음 코드 예제에서는 Amazon SES SMTP 엔드포인트에 연결할 자격 증명을 생성하는 방법을 보여줍니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

#!/usr/bin/env python3 import hmac import hashlib import base64 import argparse SMTP_REGIONS = [ "us-east-2", # US East (Ohio) "us-east-1", # US East (N. Virginia) "us-west-2", # US West (Oregon) "ap-south-1", # Asia Pacific (Mumbai) "ap-northeast-2", # Asia Pacific (Seoul) "ap-southeast-1", # Asia Pacific (Singapore) "ap-southeast-2", # Asia Pacific (Sydney) "ap-northeast-1", # Asia Pacific (Tokyo) "ca-central-1", # Canada (Central) "eu-central-1", # Europe (Frankfurt) "eu-west-1", # Europe (Ireland) "eu-west-2", # Europe (London) "eu-south-1", # Europe (Milan) "eu-north-1", # Europe (Stockholm) "sa-east-1", # South America (Sao Paulo) "us-gov-west-1", # AWS GovCloud (US) "us-gov-east-1", # AWS GovCloud (US) ] # These values are required to calculate the signature. Do not change them. DATE = "11111111" SERVICE = "ses" MESSAGE = "SendRawEmail" TERMINAL = "aws4_request" VERSION = 0x04 def sign(key, msg): return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest() def calculate_key(secret_access_key, region): if region not in SMTP_REGIONS: raise ValueError(f"The {region} Region doesn't have an SMTP endpoint.") signature = sign(("AWS4" + secret_access_key).encode("utf-8"), DATE) signature = sign(signature, region) signature = sign(signature, SERVICE) signature = sign(signature, TERMINAL) signature = sign(signature, MESSAGE) signature_and_version = bytes([VERSION]) + signature smtp_password = base64.b64encode(signature_and_version) return smtp_password.decode("utf-8") def main(): parser = argparse.ArgumentParser( description="Convert a Secret Access Key to an SMTP password." ) parser.add_argument("secret", help="The Secret Access Key to convert.") parser.add_argument( "region", help="The AWS Region where the SMTP password will be used.", choices=SMTP_REGIONS, ) args = parser.parse_args() print(calculate_key(args.secret, args.region)) if __name__ == "__main__": main()

다음 코드 예시는 다음과 같은 작업을 수행하는 방법을 보여줍니다.

  • Amazon 을 사용하여 이메일 주소를 추가하고 확인합니다SES.

  • 표준 이메일 메시지를 보냅니다.

  • 템플릿을 생성하고 템플릿 이메일 메시지를 보냅니다.

  • Amazon SES SMTP 서버를 사용하여 메시지를 보냅니다.

SDK Python용(Boto3)
참고

에 대한 자세한 내용은 를 참조하세요 GitHub. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

Amazon에서 이메일 주소를 확인하고 메시지를 SES 보냅니다.

def usage_demo(): print("-" * 88) print("Welcome to the Amazon Simple Email Service (Amazon SES) email demo!") print("-" * 88) logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") ses_client = boto3.client("ses") ses_identity = SesIdentity(ses_client) ses_mail_sender = SesMailSender(ses_client) ses_template = SesTemplate(ses_client) email = input("Enter an email address to send mail with Amazon SES: ") status = ses_identity.get_identity_status(email) verified = status == "Success" if not verified: answer = input( f"The address '{email}' is not verified with Amazon SES. Unless your " f"Amazon SES account is out of sandbox, you can send mail only from " f"and to verified accounts. Do you want to verify this account for use " f"with Amazon SES? If yes, the address will receive a verification " f"email (y/n): " ) if answer.lower() == "y": ses_identity.verify_email_identity(email) print(f"Follow the steps in the email to {email} to complete verification.") print("Waiting for verification...") try: ses_identity.wait_until_identity_exists(email) print(f"Identity verified for {email}.") verified = True except WaiterError: print( f"Verification timeout exceeded. You must complete the " f"steps in the email sent to {email} to verify the address." ) if verified: test_message_text = "Hello from the Amazon SES mail demo!" test_message_html = "<p>Hello!</p><p>From the <b>Amazon SES</b> mail demo!</p>" print(f"Sending mail from {email} to {email}.") ses_mail_sender.send_email( email, SesDestination([email]), "Amazon SES demo", test_message_text, test_message_html, ) input("Mail sent. Check your inbox and press Enter to continue.") template = { "name": "doc-example-template", "subject": "Example of an email template.", "text": "This is what {{name}} will {{action}} if {{name}} can't display " "HTML.", "html": "<p><i>This</i> is what {{name}} will {{action}} if {{name}} " "<b>can</b> display HTML.</p>", } print("Creating a template and sending a templated email.") ses_template.create_template(**template) template_data = {"name": email.split("@")[0], "action": "read"} if ses_template.verify_tags(template_data): ses_mail_sender.send_templated_email( email, SesDestination([email]), ses_template.name(), template_data ) input("Mail sent. Check your inbox and press Enter to continue.") print("Sending mail through the Amazon SES SMTP server.") boto3_session = boto3.Session() region = boto3_session.region_name credentials = boto3_session.get_credentials() port = 587 smtp_server = f"email-smtp.{region}.amazonaws.com" password = calculate_key(credentials.secret_key, region) message = """ Subject: Hi there This message is sent from the Amazon SES SMTP mail demo.""" context = ssl.create_default_context() with smtplib.SMTP(smtp_server, port) as server: server.starttls(context=context) server.login(credentials.access_key, password) server.sendmail(email, email, message) print("Mail sent. Check your inbox!") if ses_template.template is not None: print("Deleting demo template.") ses_template.delete_template() if verified: answer = input(f"Do you want to remove {email} from Amazon SES (y/n)? ") if answer.lower() == "y": ses_identity.delete_identity(email) print("Thanks for watching!") print("-" * 88)

Amazon SES 자격 증명 작업을 래핑하는 함수를 생성합니다.

class SesIdentity: """Encapsulates Amazon SES identity functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def verify_domain_identity(self, domain_name): """ Starts verification of a domain identity. To complete verification, you must create a TXT record with a specific format through your DNS provider. For more information, see *Verifying a domain with Amazon SES* in the Amazon SES documentation: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-domain-procedure.html :param domain_name: The name of the domain to verify. :return: The token to include in the TXT record with your DNS provider. """ try: response = self.ses_client.verify_domain_identity(Domain=domain_name) token = response["VerificationToken"] logger.info("Got domain verification token for %s.", domain_name) except ClientError: logger.exception("Couldn't verify domain %s.", domain_name) raise else: return token def verify_email_identity(self, email_address): """ Starts verification of an email identity. This function causes an email to be sent to the specified email address from Amazon SES. To complete verification, follow the instructions in the email. :param email_address: The email address to verify. """ try: self.ses_client.verify_email_identity(EmailAddress=email_address) logger.info("Started verification of %s.", email_address) except ClientError: logger.exception("Couldn't start verification of %s.", email_address) raise def wait_until_identity_exists(self, identity): """ Waits until an identity exists. The waiter polls Amazon SES until the identity has been successfully verified or until it exceeds its maximum time. :param identity: The identity to wait for. """ try: waiter = self.ses_client.get_waiter("identity_exists") logger.info("Waiting until %s exists.", identity) waiter.wait(Identities=[identity]) except WaiterError: logger.error("Waiting for identity %s failed or timed out.", identity) raise def get_identity_status(self, identity): """ Gets the status of an identity. This can be used to discover whether an identity has been successfully verified. :param identity: The identity to query. :return: The status of the identity. """ try: response = self.ses_client.get_identity_verification_attributes( Identities=[identity] ) status = response["VerificationAttributes"].get( identity, {"VerificationStatus": "NotFound"} )["VerificationStatus"] logger.info("Got status of %s for %s.", status, identity) except ClientError: logger.exception("Couldn't get status for %s.", identity) raise else: return status def delete_identity(self, identity): """ Deletes an identity. :param identity: The identity to remove. """ try: self.ses_client.delete_identity(Identity=identity) logger.info("Deleted identity %s.", identity) except ClientError: logger.exception("Couldn't delete identity %s.", identity) raise def list_identities(self, identity_type, max_items): """ Gets the identities of the specified type for the current account. :param identity_type: The type of identity to retrieve, such as EmailAddress. :param max_items: The maximum number of identities to retrieve. :return: The list of retrieved identities. """ try: response = self.ses_client.list_identities( IdentityType=identity_type, MaxItems=max_items ) identities = response["Identities"] logger.info("Got %s identities for the current account.", len(identities)) except ClientError: logger.exception("Couldn't list identities for the current account.") raise else: return identities

Amazon SES 템플릿 작업을 래핑하는 함수를 생성합니다.

class SesTemplate: """Encapsulates Amazon SES template functions.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client self.template = None self.template_tags = set() def _extract_tags(self, subject, text, html): """ Extracts tags from a template as a set of unique values. :param subject: The subject of the email. :param text: The text version of the email. :param html: The html version of the email. """ self.template_tags = set(re.findall(TEMPLATE_REGEX, subject + text + html)) logger.info("Extracted template tags: %s", self.template_tags) def create_template(self, name, subject, text, html): """ Creates an email template. :param name: The name of the template. :param subject: The subject of the email. :param text: The plain text version of the email. :param html: The HTML version of the email. """ try: template = { "TemplateName": name, "SubjectPart": subject, "TextPart": text, "HtmlPart": html, } self.ses_client.create_template(Template=template) logger.info("Created template %s.", name) self.template = template self._extract_tags(subject, text, html) except ClientError: logger.exception("Couldn't create template %s.", name) raise def delete_template(self): """ Deletes an email template. """ try: self.ses_client.delete_template(TemplateName=self.template["TemplateName"]) logger.info("Deleted template %s.", self.template["TemplateName"]) self.template = None self.template_tags = None except ClientError: logger.exception( "Couldn't delete template %s.", self.template["TemplateName"] ) raise def get_template(self, name): """ Gets a previously created email template. :param name: The name of the template to retrieve. :return: The retrieved email template. """ try: response = self.ses_client.get_template(TemplateName=name) self.template = response["Template"] logger.info("Got template %s.", name) self._extract_tags( self.template["SubjectPart"], self.template["TextPart"], self.template["HtmlPart"], ) except ClientError: logger.exception("Couldn't get template %s.", name) raise else: return self.template def list_templates(self): """ Gets a list of all email templates for the current account. :return: The list of retrieved email templates. """ try: response = self.ses_client.list_templates() templates = response["TemplatesMetadata"] logger.info("Got %s templates.", len(templates)) except ClientError: logger.exception("Couldn't get templates.") raise else: return templates def update_template(self, name, subject, text, html): """ Updates a previously created email template. :param name: The name of the template. :param subject: The subject of the email. :param text: The plain text version of the email. :param html: The HTML version of the email. """ try: template = { "TemplateName": name, "SubjectPart": subject, "TextPart": text, "HtmlPart": html, } self.ses_client.update_template(Template=template) logger.info("Updated template %s.", name) self.template = template self._extract_tags(subject, text, html) except ClientError: logger.exception("Couldn't update template %s.", name) raise

Amazon SES 이메일 작업을 래핑하는 함수를 생성합니다.

class SesDestination: """Contains data about an email destination.""" def __init__(self, tos, ccs=None, bccs=None): """ :param tos: The list of recipients on the 'To:' line. :param ccs: The list of recipients on the 'CC:' line. :param bccs: The list of recipients on the 'BCC:' line. """ self.tos = tos self.ccs = ccs self.bccs = bccs def to_service_format(self): """ :return: The destination data in the format expected by Amazon SES. """ svc_format = {"ToAddresses": self.tos} if self.ccs is not None: svc_format["CcAddresses"] = self.ccs if self.bccs is not None: svc_format["BccAddresses"] = self.bccs return svc_format class SesMailSender: """Encapsulates functions to send emails with Amazon SES.""" def __init__(self, ses_client): """ :param ses_client: A Boto3 Amazon SES client. """ self.ses_client = ses_client def send_email(self, source, destination, subject, text, html, reply_tos=None): """ Sends an email. Note: If your account is in the Amazon SES sandbox, the source and destination email accounts must both be verified. :param source: The source email account. :param destination: The destination email account. :param subject: The subject of the email. :param text: The plain text version of the body of the email. :param html: The HTML version of the body of the email. :param reply_tos: Email accounts that will receive a reply if the recipient replies to the message. :return: The ID of the message, assigned by Amazon SES. """ send_args = { "Source": source, "Destination": destination.to_service_format(), "Message": { "Subject": {"Data": subject}, "Body": {"Text": {"Data": text}, "Html": {"Data": html}}, }, } if reply_tos is not None: send_args["ReplyToAddresses"] = reply_tos try: response = self.ses_client.send_email(**send_args) message_id = response["MessageId"] logger.info( "Sent mail %s from %s to %s.", message_id, source, destination.tos ) except ClientError: logger.exception( "Couldn't send mail from %s to %s.", source, destination.tos ) raise else: return message_id def send_templated_email( self, source, destination, template_name, template_data, reply_tos=None ): """ Sends an email based on a template. A template contains replaceable tags each enclosed in two curly braces, such as {{name}}. The template data passed in this function contains key-value pairs that define the values to insert in place of the template tags. Note: If your account is in the Amazon SES sandbox, the source and destination email accounts must both be verified. :param source: The source email account. :param destination: The destination email account. :param template_name: The name of a previously created template. :param template_data: JSON-formatted key-value pairs of replacement values that are inserted in the template before it is sent. :return: The ID of the message, assigned by Amazon SES. """ send_args = { "Source": source, "Destination": destination.to_service_format(), "Template": template_name, "TemplateData": json.dumps(template_data), } if reply_tos is not None: send_args["ReplyToAddresses"] = reply_tos try: response = self.ses_client.send_templated_email(**send_args) message_id = response["MessageId"] logger.info( "Sent templated mail %s from %s to %s.", message_id, source, destination.tos, ) except ClientError: logger.exception( "Couldn't send templated mail from %s to %s.", source, destination.tos ) raise else: return message_id