Proteeti Prova
Proteeti Prova

Reputation: 1169

AWS - Using email template from S3 bucket

I can send email with amazon SES in python with boto3. I made my email template and passing it as a parameter inside my code. I want to upload my email template in S3 bucket and intergrate it with my existing code. I have searched the documentation but can't find any lead. How do I do this? Here is my code so far:

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)

Upvotes: 1

Views: 2875

Answers (2)

Proteeti Prova
Proteeti Prova

Reputation: 1169

Basically I had to fetch the file from s3 as an object, which I did following this. I added these into my code:

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

Upvotes: 4

WalKh
WalKh

Reputation: 502

You can create a template which will be stored in AWS and then you can use send_templated_email to use a template and render it , in case you want to customise it with variables.

Upvotes: 0

Related Questions