python 發郵件
問題描述
用python 發送帶zip格式的郵件,郵件發送成功了,但是附件打不開是,代碼如下
import smtplibfrom email import encodersfrom email.mime.base import MIMEBasefrom email.mime.multipart import MIMEMultipart
def send_file_zipped(the_file):
themsg = MIMEMultipart()themsg[’Subject’] = the_filethemsg[’to’] = ’xxx’themsg[’from’] = ’xxx’themsg.preamble = the_filemsg = MIMEBase(’application’, ’zip’)zf = open(the_file + ’.zip’, ’rb’)msg.set_payload(zf.read())encoders.encode_base64(msg)msg.add_header(’Content-Disposition’, ’attachment’, filename=the_file + ’.zip’)themsg.attach(msg)themsg = themsg.as_string()try: server = smtplib.SMTP() server.timeout = 30 server.connect(’smtp.exmail.qq.com’) server.login(’xxx’, ’xxx’) server.sendmail(’xxx’, ’xxx’, themsg) server.quit() print ’發送成功’except Exception, e: print str(e)
if name == '__main__':
file = ’20170305’send_file_zipped(file)
找了好多方法,都是這個結果,請教各位是哪里出了問題,郵件附件顯示如下:
問題解答
回答1:你可以試一下我寫的這個,用的是新浪郵箱發的,在我這兒是無論什么附件格式都可以發
相關文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂3. python - 編碼問題求助4. python - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處7. mysql - jdbc的問題8. python - Scrapy存在內存泄漏的問題。9. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?10. mysql - 千萬級數據的表,添加unique約束,insert會不會很慢?
