Python AWS lambda通过SES发送带有附件的电子邮件>;ParamValidationError:参数验证失败
我使用AWS lambda通过Amazon SES发送带有附件的电子邮件。存储在S3存储桶中的附件文件。有没有人有使用lambda和通过ses通过lambda函数发送电子邮件的经验 这是我的密码:Python AWS lambda通过SES发送带有附件的电子邮件>;ParamValidationError:参数验证失败,python,amazon-web-services,amazon-ses,aws-lambda,Python,Amazon Web Services,Amazon Ses,Aws Lambda,我使用AWS lambda通过Amazon SES发送带有附件的电子邮件。存储在S3存储桶中的附件文件。有没有人有使用lambda和通过ses通过lambda函数发送电子邮件的经验 这是我的密码: import boto3 from email.mime.text import MIMEText from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart se
import boto3
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
ses = boto3.client('ses')
s3_client = boto3.client('s3')
to_emails = ["xxx", "xxx"]
COMMASPACE = ', '
def lambda_handler(event, context):
# create raw email
msg = MIMEMultipart()
msg['Subject'] = 'Email subject'
msg['From'] = 'xxx'
msg['To'] = COMMASPACE.join(to_emails) ## joined the array of email strings
# edit: didn't end up using this ^
part = MIMEText('Attached is an important CSV')
msg.attach(part)
file_name = s3_client.download_file('x_file', 'x.jpg', '/tmp/x.jpg')
if file_name:
part = MIMEApplication(open(file_name, "rb").read())
part.add_header('Content-Disposition', 'attachment', filename=file_name)
msg.attach(part)
ses.send_raw_email(RawMessage=msg.as_string(), Source=msg['From'], Destinations=to_emails)
发现此错误:
参数验证失败:
参数RawMessage的类型无效,值:内容类型:多部分/混合;边界=“======================1951926695068149774==”
MIME版本:1.0
主题:电子邮件主题
发件人:xxx
致:xxx,xxx
--===============1951926695068149774==
内容类型:文本/纯文本;charset=“us ascii”
MIME版本:1.0
内容传输编码:7bit
附件是一个重要的CSV
--===============1951926695068149774==--
,类型:,有效类型::ParamValidationError
回溯(最近一次呼叫最后一次):
lambda_处理程序中的第33行“/var/task/lambda_function.py”文件
ses.send_raw_email(RawMessage=msg.as_string(),Source=msg['From'],Destinations=to_email)
文件“/var/runtime/botocore/client.py”,第278行,在api调用中
返回self.\u make\u api\u调用(操作名称,kwargs)
文件“/var/runtime/botocore/client.py”,第548行,在make\u api\u调用中
api参数,操作模型,上下文=请求(上下文)
文件“/var/runtime/botocore/client.py”,第601行,在“转换为请求”目录中
api(参数、操作(模型)
文件“/var/runtime/botocore/validate.py”,第270行,在序列化请求中
raise ParamValidationError(report=report.generate_report())
ParamValidationError:参数验证失败:
参数RawMessage的类型无效,值:内容类型:多部分/混合;边界=“======================1951926695068149774==”
MIME版本:1.0
主题:电子邮件主题
发件人:xxx
致:xxx,xxx
--===============1951926695068149774==
内容类型:文本/纯文本;charset=“us ascii”
MIME版本:1.0
内容传输编码:7bit
附件是一个重要的CSV
--===============1951926695068149774==--
,类型:,有效类型:
要解决此问题,您应该使用:
ses.send_raw_email(RawMessage={
'Data': msg.as_string(),
},
Source=msg['From'],
Destinations=to_emails
)
如中所述,从EC2实例运行此代码时会发生什么情况?
ses.send_raw_email(RawMessage={
'Data': msg.as_string(),
},
Source=msg['From'],
Destinations=to_emails
)