我正在使用python 2.7和boto3。 我无法想出一种在python中添加附件到SES的方法。 我找到的最接近的是this page。
到目前为止,我所拥有的是:
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
“email”和“other_email”受到审查,但采用字符串格式“e@mail.xxx”。 该地址通过AWS授权,密钥和密钥已通过boto3实现。
也在输出的底部得到了这个:
type: <type 'instance'>, valid types: <type 'dict'>
Invalid type for parameter Destinations,
value: other_email,
type: <type 'str'>, valid types: <type 'list'>, <type 'tuple'>
答案 0 :(得分:3)
我明白了!可能有更好的方法来做到这一点,但它对我有用。请让我知道如何改进这一点。谢谢。
新代码:
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