通过python发送附带文件

#-*- coding: utf-8 -*-
#/usr/bin/env python


from email.Header import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
import smtplib,datetime


#创建一个带附件的实例
msg = MIMEMultipart()


#构造附件
att = MIMEText(open('a.rar','rb').read(),'base64','gb2321')
#附件类型
att['Content-Type'] = 'application/octet-stream'
att['Content-Disposition'] = 'attachment;filename = "a.rar"'
msg.attach(att) #raise the MultipartConversionError exception
#msg['to'] = '[email protected]'
#msg['from'] = '[email protected]'
msg['subject'] = Header('冒烟测试结果(' + str(datetime.date.today()) + ')','iso-8859-1')
try:
        s = smtplib.SMTP()
        s.connect('smtp.qq.com')
        s.login('*******@qq.com','你的密码')
        s.sendmail('******@qq.com','*******@qq.com',msg.as_string())
        s.close()
except Exception,e:
        print str(e)

你可能感兴趣的:(exception,python,String,header,测试,import)