lakshmi
lakshmi

Reputation: 211

how to pass variables in the email body from lambda aws -python

I am trying to send email from lambda by passing variable in the html body inside payload(in send_email()).I tried using +str(latesturgency)+ and also using {latestimpact} .This doesn.t work.I am new to lambda and python.

How can I solve this?

import json
import logging
import re
import http.client
import mimetypes

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
    
    conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
   
    payload = {
        "from":"[email protected]",
        "to": "[email protected]",
        "subject": "Test mail","textbody": "body",
        "htmlbody": "<h3>Test body!Please create a +str(latesturgency)+ priority ticket to {latestservice}  , with below message:{latestdescription}</h3>"
        
    }
    print(payload)
    headers = {
     'Content-Type': 'application/json'
    }
    data=json.dumps(payload)
    print(data)
    conn.request("POST", "", data, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    print("done")



def lambda_handler(event, context):

    empidemployee= event['currentIntent']["slots"]["empidemployee"]
    latestdescription= event['currentIntent']["slots"]["latestdescription"]
    latestservice= event['currentIntent']["slots"]["latestservice"]
    latestimpact= event['currentIntent']["slots"]["latestimpact"]
    latesturgency= event['currentIntent']["slots"]["latesturgency"]
    email=event['currentIntent']["slots"]["email"]

    send_email(email,latestdescription,latestservice,latestimpact,latesturgency)
   

Upvotes: 1

Views: 782

Answers (1)

Chris Williams
Chris Williams

Reputation: 35238

You can do this using the format function fro your string, I have added this below.

import json
import logging
import re
import http.client
import mimetypes

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)


def send_email(email,latestdescription,latestservice,latestimpact,latesturgency):
    
    conn = http.client.HTTPSConnection("mail.us-east-1.aws.cloud.bmw")
   
    payload = {
        "from":"[email protected]",
        "to": "[email protected]",
        "subject": "Test mail","textbody": "body",
        "htmlbody": "<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)
        
    }
    print(payload)
    headers = {
     'Content-Type': 'application/json'
    }
    data=json.dumps(payload)
    print(data)
    conn.request("POST", "", data, headers)
    res = conn.getresponse()
    data = res.read()
    print(data.decode("utf-8"))
    print("done")



def lambda_handler(event, context):

    empidemployee= event['currentIntent']["slots"]["empidemployee"]
    latestdescription= event['currentIntent']["slots"]["latestdescription"]
    latestservice= event['currentIntent']["slots"]["latestservice"]
    latestimpact= event['currentIntent']["slots"]["latestimpact"]
    latesturgency= event['currentIntent']["slots"]["latesturgency"]
    email=event['currentIntent']["slots"]["email"]

    send_email(email,latestdescription,latestservice,latestimpact,latesturgency)

Use the {} notation to indicate where a variable should be replace then use .format() after your string passing in the variables in the ordering you will use them in.

In your case it is now

"<h3>Test body!Please create a {} priority ticket to {}, with below message:{}</h3>".format(latesturgency, latestservice, latestdescription)

For more information about the format function take a read in the documentation.

Upvotes: 1

Related Questions