簡體   English   中英

在lambda function中,如何在HTML中顯示python變量

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

在下面的 lambda function 中,我想在 HTML “BODY_HTML”中傳遞“Message2”變量。 有沒有辦法在 HTML 中傳遞我的事件 Message2。

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

字符編碼為email。

    CHARSET = "UTF-8"

創建新的 SES 資源並指定區域。

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

嘗試發送 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:"),
    

嘗試使用字符串格式:

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

或字符串連接:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM