我正在尝试使用python smtp库发送电子邮件。一切似乎工作正常,但我正在重复最后一个附件,同时尝试发送两个附件我最后重复三个。我想弄清楚这个但是不能。这是我的消息编写功能的代码片段:
def create_email_message(sender_address, recipient_address, subject, body, filesin=[]):
'''
Creates a ready to go email message along with the headers.
@param: filesin
A list of tuples. Example:
[(file1_name,file1_data),(file2_name,file2_data),.......]
'''
msg = MIMEMultipart('alternative')
msg['Subject'] = "%s" % Header(subject, 'utf-8')
msg['From'] = sender_address
msg['To'] = recipient_address
htmlpart = MIMEText(body, 'html', 'UTF-8')
msg.attach(htmlpart)
for file_obj in filesin:
data = file_obj[1]
name = file_obj[0]
attach_obj = MIMEBase("application", "octet-stream")
attach_obj.set_payload(data)
Encoders.encode_base64(attach_obj)
attach_obj.add_header('Content-Disposition', 'attachment',
filename=name)
msg.attach(attach_obj)
return msg.as_string()
filesin 参数是一个元组,如函数定义所示。最奇怪的部分是,当我查看原始电子邮件时,gmail只显示两个内容处置标题。无法弄清楚最后一个附件重复的原因。
问候。