在带有Boto3 / Amazon SES的HTML BODY中使用列表值

问题描述 投票:0回答:1

我有以下问题。我使用boto3通过Amazon SES生成HMTL电子邮件。在这里我要使用占位符。我有一个列表,其中包含要在此占位符中放入的值,但我不知道如何执行此操作。每个占位符都有一个值,一切正常。

我的代码如下:

import boto3
SENDER = "[email protected]"
RECIPIENT = "[email protected]"
AWS_REGION = "yyy"
SUBJECT = "Title"
BODY_TEXT = ("test\r\n"
             "Dear Sir,\n"
             "please give me your feedback:\n"
             )
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:[email protected]">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,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            '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'])

我的列表如下:

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

当前输出看起来像这样:

**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

如何获得其他值?我想要这样的输出:

**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
python boto3 amazon-ses
1个回答
0
投票

好吧,我明白了。我创建一个辅助函数:

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
© www.soinside.com 2019 - 2024. All rights reserved.