我们正在尝试创建一个 AWS Lambda 函数来创建一封带有 HTML 正文的电子邮件,以提醒团队有关更改的记录。为了实现这一目标,我们使用 Python 创建了一个 Lambda 函数,该函数从事件响应中捕获变量并传递到 HTML。我下面的代码在从 HTML 到 python 的变量方面存在问题。你能帮我解决它吗?由于发送的电子邮件只是 HTML 中提到的字符串。一般来说,我应该获取分配给它的变量。import boto3import jsonfrom datetime import datetimeimport smtplib import email.utilsfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextdef lambda_handler(event, context): #print(event) RecordInfo = event['Records'] RecordDetails = RecordInfo[0] EventName = RecordDetails['eventName'] Dyna = RecordDetails['dynamodb'] Modtime = Dyna['ApproximateCreationDateTime'] ModifiedTime=datetime.fromtimestamp(Modtime).strftime("%B %d, %Y %I:%M:%S") if (EventName == 'MODIFY'): OldData = Dyna['OldImage'] OldAppKey=OldData['Application'] OldApp=OldAppKey['S'] OldAppServerKey=OldData['App_Server'] OldAppServer=OldAppServerKey['S'] OldAppUserKey=OldData['App_User'] OldAppUser=OldAppUserKey['S'] OldAppIPKey=OldData['App_IP'] OldAppIP=OldAppIPKey['S'] OldAppPwdKey=OldData['App_Pwd'] OldAppPwd=OldAppPwdKey['S'] OldAppPortKey=OldData['App_Port'] OldAppPort=OldAppPortKey['S'] #If Eventname is 'Modify' then above inserted value of Sub will be overridden # The subject line of the email. SUBJECT = "Table Name:DBTest - Updated on " + ModifiedTime Message1 = Message1 + "\nOld Record Details:\n" + "Application:" + OldApp + "\tApp_IP:" + OldAppIP + "\tApp_User:" + OldAppUser + "\tApp_Pwd:" + OldAppPwd + "\tApp_Port:" + OldAppPort
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
看来您希望用这样的字符串替换变量:
<td class="cell">{OldApp}</td>
要在 Python 3.6+ 中执行此操作,您应该使用f-string
,例如:
BODY_HTML = f"...<td class="cell">{OldApp}</td>..."
请注意f
开头引号之前的内容,它告诉 Python 替换{curly brackets}
.
另外,该print()
语句不应该出现在这个变量赋值中:
BODY_HTML = BODY_HTML + print ("...
另外一些提示..
你可以缩短这个:
RecordInfo = event['Records'] RecordDetails = RecordInfo[0] EventName = RecordDetails['eventName']
进入:
EventName = event['Records'][0]['eventName']
同样,你可以缩短:
OldAppIPKey=OldData['App_IP'] OldAppIP=OldAppIPKey['S']
进入:
OldAppIP=OldData['App_IP']['S']
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消