Search code examples
pythonboto3amazon-ses

Use List Values in HTML BODY with Boto3/Amazon SES


I have the following problem. I use boto3 to generate a HMTL EMail with Amazon SES. Here I want to use Placeholders. I have a list with values which I want to put in this placeholders, but I dont know how to do this. With one value for each placeholder everything works fine.

My Code looks like this:

import boto3
SENDER = "xy@abc.com"
RECIPIENT = "rec@abc.com"
AWS_REGION = "yyy"
SUBJECT = "Title"
BODY_HTML = """<html>
           <head></head>
               <body>
               <h3>Header</h3>
               <p>Dear Sir,</p>
               <p>please give me your feedback to these products:</p>
               <p>Number: {Number}, Text: {Text}, Count: {Count}</p>
               <p>Best regards</p>
               <p>S.O</p>
               <p>Company</p>
               <p>S.O</p>
               <p>Street and City</p>
               <p>Phone</p>
               <p>Fax</p>
               <p>E-Mail: <a href="mailto:example@abc.com">Abcabc.com</a></p>
               <p>Web: <a href="www.stackoverflow.com">www.stackoverflow.com</a></p>
           </body>
           </html>
               """
for x in i:
    new_body = BODY_HTML.format(Number=x[3], Text=x[1], Count=x[2])

CHARSET = "UTF-8"
client = boto3.client('ses', aws_access_key_id="",
                      aws_secret_access_key="", region_name=AWS_REGION)
try:
    response = client.send_email(
        Destination={
            'ToAddresses': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': new_body,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            }
        },
        Source=SENDER,
    )
except ClientError as z:
    print(z.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])

My list looks like this:

i = [(120, 'ABC', 12, 12345),(121,'BCD',13,253461),(122,'FFG',16,564895)]

The current output looks like this:

**Header**
Dear Sir,
please give me your feedback to these products:
Number: 564895, Text: FFG, Count: 16
Best regards
S.O
Company
S.O
Street and City
Phone
Fax
E-Mail: Abcabc.com
Web: www.stackoverflow.com

How can I get the other values? I want a output like this:

**Header**
Dear Sir,
please give me your feedback to these products:
Number: 564895, Text: FFG, Count: 16
Number: 12345, Text: ABC, Count: 12 
Number: 123461, Text: BCD, Count: 13
Best regards
S.O
Company
S.O
Street and City
Phone
Fax
E-Mail: Abcabc.com
Web: www.stackoverflow.com

Solution

  • Ok I got it. I create a helperfunction:

    def helper(elements):
    string = ""
    for s in elements:
        a = ''.join(str(s[3]))
        b = ''.join(str(s[1]))
        c = ''.join(str(s[2]))
        string += "<p>"+"Number: "+a+", ""Text: "+b+", ""Count: "+c
        string += "</p>\n"
    return string