В лямбда-функции, как отобразить python переменную в HTML - PullRequest
0 голосов
/ 04 августа 2020

В приведенной ниже лямбда-функции я хочу передать переменную «Message2» в HTML «BODY_ HTML». Есть ли способ передать мое событие Message2 в 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>
               """** 

Кодировка символов для электронной почты.

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

1 Ответ

1 голос
/ 04 августа 2020

Попробуйте использовать форматирование строк:

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

Или объединение строк:

BODY_HTML = f"<html><head></head><body><p>" + Message2 + "</p></body></html>"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...