【Python】傳送帶文字圖片附件的郵件
阿新 • • 發佈:2019-01-03
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
HOST = "smtp.gmail.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("<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"))
attach = MIMEText(open("doc/week_report.xlsx", "rb").read(), "base64", "utf-8")
attach["Content-Type"] = "application/octet-stream"
#attach["Content-Disposition"] = "attachment; filename=\"業務服務質量週報(12周).xlsx\"".decode("utf-8").encode("gb18030")
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]","123456")
server.sendmail(FROM, TO, msg.as_string())
server.quit()
print "郵件傳送成功!"
except Exception, e:
print "失敗:"+str(e)