使用Python的SES附件 [英] SES Attachments with Python

查看:236
本文介绍了使用Python的SES附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python 2.7和boto3. 我想不通一种在python中向SES添加附件的方法. 我发现最接近的东西是此页面.

I'm using python 2.7 and boto3. I can not figure out a way to add attachments to SES in python. The closest thing I found was this page.

到目前为止,我所拥有的是:

So far what I have is this:

from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import boto3

# via http://codeadict.wordpress.com/2010/02/11/send-e-mails-with-attachment-in-python/
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = email
msg['To'] = other_email

# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'

# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)

# the attachment
part = MIMEApplication(open('cat.jpg', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='cat.jpg')
msg.attach(part)

result = ses.send_raw_email(
    Source=msg['From'],
    Destinations=msg['To'],
    RawMessage=msg
)                                                                                                       
# and send the message
print result

我得到:

ParamValidationError: Parameter validation failed:
Invalid type for parameter RawMessage, value: From nobody Tue Jul 25 11:21:41 2017
Content-Type: multipart/mixed; boundary="===============0285276385=="
MIME-Version: 1.0
Subject: weekly report
From: email
To: other_email

对电子邮件"和"other_email"进行了审查,但其字符串格式为"e@mail.xxx". 该地址已通过AWS授权,并且Key和Secret密钥已通过boto3实现.

"email" and "other_email" are censored but in String format 'e@mail.xxx'. The address is authorized through AWS and the Key and Secret key are already implemented through boto3.

也将其放在输出的底部:

Also got this at the bottom of the output:

type: <type 'instance'>, valid types: <type 'dict'>
Invalid type for parameter Destinations,
value: other_email, 
type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>

推荐答案

我知道了!可能有更好的方法可以执行此操作,但是它对我有用.请让我知道如何对此进行改进.谢谢.

I figured it out! There are probably better ways of doing this, but it worked for me. Please let me know how to improve on this. Thank you.

新代码:

to_emails = [target_email1, target_email2]
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = from_email
msg['To'] = to_emails[0]

# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'

# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)

# the attachment
part = MIMEApplication(open('cat.jpg', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename='cat.jpg')
msg.attach(part)

result = ses.send_raw_email(
    Source=msg['From'],
    Destinations=to_emails,
    RawMessage={'Data': msg.as_string()}
)                                                                                                       
# and send the message
print result

这篇关于使用Python的SES附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆