1

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)
   

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.