AWS - 使用来自 S3 存储桶的电子邮件模板

AWS - Using email template from S3 bucket

我可以使用 boto3 在 python 中使用亚马逊 SES 发送电子邮件。我制作了我的电子邮件模板并将其作为参数传递到我的代码中。我想在 S3 存储桶中上传我的电子邮件模板并将其与我现有的代码集成。我已经搜索了文档,但找不到任何线索。我该怎么做呢?到目前为止,这是我的代码:

import boto3
from botocore.exceptions import ClientError
SENDER = "************"
RECIPIENT = "*************"
AWS_REGION = "us-east-1"
SUBJECT = "Amazon SES Test (SDK for Python)"
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
             "This email was sent with Amazon SES using the "
             "AWS SDK for Python (Boto)."
            )

BODY_HTML = """<html>
<head></head>
<body>
  <h1>Amazon SES Test (SDK for Python)</h1>
  <p>This email was sent with
    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
    <a href='https://aws.amazon.com/sdk-for-python/'>
      AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
            """            

CHARSET = "UTF-8"
client = boto3.client('ses',aws_access_key_id='**',
                              aws_secret_access_key='**',region_name='us-east-1')
s3_client = boto3.client('s3',aws_access_key_id='**',
                              aws_secret_access_key='***',region_name='us-east-1')
try:
    #Provide the contents of the email.
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            },
        },
        Source=SENDER,
    )   
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])
    print(s3_client)

您可以create a template which will be stored in AWS and then you can use send_templated_email使用模板并渲染它,以防您想使用变量对其进行自定义。

基本上我必须从 s3 中获取文件作为对象,我在 之后就这样做了。我将这些添加到我的代码中:

 s3_response_object = s3_client.get_object(Bucket='bucket name', Key='template.html')
object_content = s3_response_object['Body'].read()
BODY_HTML = object_content