在lambda函数中,如何在HTML中显示python变量

2024-09-25 02:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的lambda函数中,我想在HTML“BODY_HTML”中传递“Message2”变量。是否有方法在HTML中传递我的事件消息2

import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):    
    Message1 = event ["Message"]
    print("Message_Received : ")
    print(Message1)
    Message2 = event ["Event"]
    print("Event_Received : ")
    print(Message2)
    Message3 = event ["Sender"]
    print("Sender : ")
    print(Message3)

    SENDER = "Redshift Pause Resuem Service <redshift@abc.com>"
    RECIPIENT = Message3
    #CONFIGURATION_SET = "ConfigSet"
    AWS_REGION = "us-east-1"
    SUBJECT = subject1
    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>
               <p>I want to print here the variable "Message2"</p>
               </body>
               </html>
               """** 

电子邮件的字符编码

    CHARSET = "UTF-8"

创建新的SES资源并指定区域

    client = boto3.client('ses',region_name=AWS_REGION)

尝试发送电子邮件

    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,       
    )
    # Display an error if something goes wrong. 
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        print("Email sent! Message ID:"),
    

Tags: theclientawseventmessagedataemailhtml