2017-07-25 29 views
1

我正在使用python 2.7和boto3。 我想不出在python中添加附件到SES的方法。 我發現的最接近的是this page使用Python的SES附件

到目前爲止,我有什麼是這樣的:

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」 的審查,但在字符串格式 '[email protected]' 。 該地址通過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

我想通了!這可能有更好的方法,但它對我有效。請讓我知道如何改善這一點。謝謝。

新代碼:

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