2
我有下面的代碼,當我用它發送一個txt文件,圖像或音頻時,它的工作狀態非常好。但是,當我嘗試發送zip文件,rar文件或沒有自己的MIME(與MIMEText,MIMEImage或MIMEAudio無關)的任何其他文件時,它不起作用。將一個zip文件添加到電子郵件
總之,每當我到了其他部分(MIMEBase命令)我做錯事並且得到錯誤:
e.send_mail(TARGET, SUBJECT, "file.zip")
msg.attach(part) //two lines after the else's end
AttributeError: 'str' object has no attribute 'append'
代碼:
def send_mail(self, target, subject, *file_names):
"""
send a mail with files to the target
@param target: send the mail to the target
@param subject: mail's subject
@param file_names= list of files to send
"""
msg = email.MIMEMultipart.MIMEMultipart()
msg['From'] = self.mail
msg['To'] = email.Utils.COMMASPACE.join(target)
msg['Subject'] = subject
for file_name in file_names:
f = open(file_name, 'rb')
ctype, encoding = mimetypes.guess_type(file_name)
if ctype is None or encoding is not None:
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
# in case of a text file
if maintype == 'text':
part = MIMEText(f.read(), _subtype=subtype)
# in case of an image file
elif maintype == 'image':
part = MIMEImage(f.read(), _subtype=subtype)
# in case of an audio file
elif maintype == 'audio':
part = MIMEAudio(f.read(), _subtype=subtype)
# any other file
else:
part = MIMEBase(maintype, subtype)
msg.set_payload(f.read())
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file_name))
msg.attach(part)
f.close()
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
self.server_ssl.sendmail(self.mail, target, msg.as_string())
#server_ssl.quit()
self.server_ssl.close()
我見過類似的代碼,但我不明白我的問題。
你能解釋一下我搞砸了嗎?
謝謝!
TNX你試圖幫助我。但是,其他人甚至沒有看到我的問題。我發誓,花了不到5秒,直到有人沒有閱讀它而投下我的問題。 – tzadok
@tzadok沒人downvoted你...你有2個upvotes – paper1111
我們在談論其他一些問題我問 – tzadok