python 发送带附件的邮件


#/usr/bin/env python
#coding=utf-8
#
#from_addr = '[email protected]'
#password = 'xxx'
#to_addr = '[email protected]'
#smtp_server = 'smtp.163.com'

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

HOST = "smtp.163.com"
SUBJECT = u"官网业务服务质量周报"
TO = "[email protected]"
FROM = "[email protected]"

def addimg(src,imgid):
    fp = open(src, 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', imgid)
    return msgImage

msg = MIMEMultipart('related')
msgtext = MIMEText("官网业务周平均延时图表:

详细内容见附件。
","html","utf-8") msg.attach(msgtext) msg.attach(addimg("weekly.png","weekly")) attach = MIMEApplication(open('曾柏超的简历.doc','rb').read()) attach.add_header('Content-Disposition', 'attachment', filename="曾柏超的简历.doc") msg.attach(attach) msg['Subject'] = SUBJECT msg['From']=FROM msg['To']=TO try: server = smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login("[email protected]","xxxx") server.sendmail(FROM, TO, msg.as_string()) server.quit() print "邮件发送成功!" except Exception, e: print "失败:"+str(e)

你可能感兴趣的:(python 发送带附件的邮件)