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

时间:2019-07-22 10:01:30

标签: amazon-s3 boto3 amazon-ses

我可以使用boto3在python中使用Amazon 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)

2 个答案:

答案 0 :(得分:0)

您可以create a template(将存储在AWS中),然后使用send_templated_email使用模板并呈现它,以防您想要使用变量对其进行自定义。

答案 1 :(得分:0)

基本上,我必须从s3提取文件作为对象,我遵循this进行了此操作。我将它们添加到我的代码中:

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