繁体   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