发送SES模板电子邮件返回“在尝试传递TemplateData中的值时,模板数据无效

问题描述

在这种情况下提供帮助。我正在准备一个具有“ for”循环的脚本,该脚本将按用户基础工作。我只想使用SES模板向每个用户发送邮件

如何将用户名密码年龄传递给TemplateData中的脚本。

email_alert = ses.send_templated_email(
Source=email_from,Destination={
'ToAddresses': [
'user_mail',]
},Template='SES-TEMPLATE-Password-Warning',TemplateData='{"name": "username","password_expires": "password_expires_in"}'
)*

其中“用户名”是可以使用“ row ['user']”从excel文件提取的IAM用户名,我使用以下公式计算了年龄,我只需要在上面的模板中添加这些值数据。

last_changed_date=dateutil.parser.parse(row['password_last_changed']).date()
expires = (last_changed_date + datetime.timedelta(MaxPasswordAge)) - datetime.date.today()
password_expires_in = expires.days

解决方法

我通过添加两行代码将所需的值row['user']password_expires转换为字符串。即,解决了该问题。 user_name=str(row['user'])password_expires_in = str(password_expires),然后在模板数据中使用格式"'+user_name+'""'+password_expires_in+'"传递值。

现在此处使用的SES模板数据格式为:

TemplateData='{"name": "'+user_name+'","password_expires": "'+password_expires_in+'"}')