简体   繁体   中英

In the lambda function, how to display python variable in HTML

In the below lambda function, I want to pass "Message2" variable in the HTML "BODY_HTML". Is there a way to pass my event Message2 in the HTML.

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

The character encoding for the email.

    CHARSET = "UTF-8"

Create a new SES resource and specify a region.

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

Try to send the email.

    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:"),
    

Try using string formatting:

BODY_HTML = f"""<html>
                <head></head>
               <body>
               <p>{Message2}</p>
               </body>
               </html>
               """

Or string concatenation:

BODY_HTML = f"<html><head></head><body><p>" + Message2 + "</p></body></html>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM