sendmail-attachment.py——发送带附件的邮件

#!/usr/bin/env pythonhtml

#coding:utf-8python

import smtplibapp

from email.mime.multipart import MIMEMultipartide

from email.mime.text import MIMETextui

from email.mime.p_w_picpath import MIMEImage编码

HOST = "smtp.126.com"code

SUBJECT = u"官网业务服务质量周报"server

TO = "to@qq.com"htm

FROM = "from@126.com"对象

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("<font color=red>官网业务周平均延时图表:<br><img src=\"cid:weekly\" border=\"1\"><br>详细内容见附件。</font>","html","utf-8")

msg.attach(msgtext)

msg.attach(addimg("img/weekly.png", "weekly"))

# 建立一个MIMEText对象,附加week_report.xlsx文档

attach = MIMEText(open("doc/week_report.xlsx", "rb").read(), "base64", "utf-8")

attach["Content-Type"] = "application/octet-stream"    # 指定文件格式类型

# 指定Content-Disposition值为p_w_upload则出现下载保存对话框,保存的默认文件名使用filename指定

# 因为qqmail使用gb18030页面编码,为保证中文文件名不出现乱码,对文件名进行编码转换

attach["Content-Disposition"] = "p_w_upload; filename=\"业务服务质量周报(12周).xlsx\"".decode("utf-8").encode("gb18030")

msg.attach(attach)

msg['Subject'] = SUBJECT

msg['From'] = FROM

msg['To'] = TO

try:

    server = smtplib.SMTP(HOST, "25")

    server.starttls()

    server.login("from@126.com", "passwd")

    server.sendmail(FROM, TO, msg.as_string())

    server.quit()

    print "邮件发送成功!"

except Exception, e:

    print "失败: "+str(e)

相关文章
相关标签/搜索