#!/usr/bin/env python #coding: utf-8 by guozheng_li #该脚本是以图片作为内容发送的,没加入该说明前测试是正常可用的。 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.image import MIMEImage mailto_list=[‘745292907@qq.com‘,‘guozheng_li@quantone.com‘] mail_host="smtp.163.com" #设置服务器 mail_user="XXXXXX" #用户名 mail_pass="XXXXXX" #口令 mail_postfix="163.com" #发件箱的后缀 def send_mail(to_list,sub): def addimg(src,imgid): #文件路径、图片id fp = open(src, ‘rb‘) #打开文件 msgImage = MIMEImage(fp.read()) #读入 msgImage 中 fp.close() #关闭文件 msgImage.add_header(‘Content-ID‘, imgid) return msgImage msg = MIMEMultipart(‘related‘) #HTML代码 msgtext = MIMEText(""" <table width="600" border="0" cellspacing="0" cellpadding="4"> <tr bgcolor="#CECFAD" height="20" style="font-size:14px"> <td colspan=2>*官网性能数据 <a href="monitor.domain.com">更多>></a></td> </tr> <tr bgcolor="#EFEBDE" height="100" style="font-size:13px"> <td> <img src="cid:io"></td><td> <img src="cid:key_hit"></td> </tr> <tr bgcolor="#EFEBDE" height="100" style="font-size:13px"> <td> <img src="cid:men"></td><td> <img src="cid:swap"></td> </tr> </table>""","html","utf-8") msg.attach(msgtext) msg.attach(addimg("a.png","io")) #全文件路径,后者为ID 根据ID在HTML中插入的位置 msg.attach(addimg("a.png","key_hit")) #同上 msg.attach(addimg("a.png","men"))#同上 msg.attach(addimg("a.png","swap"))#同上 me="163"+"<"+mail_user+"@"+mail_postfix+">" msg[‘Subject‘] = sub #主题 msg[‘From‘] = me #发件人 msg[‘To‘] = ";".join(to_list) #收件人列表 try: server = smtplib.SMTP() #SMTP server.connect(mail_host) #连接 server.login(mail_user,mail_pass) #登录 server.sendmail(me, to_list, msg.as_string()) #发送邮件 server.close() #关闭 return True except Exception, e: print str(e) return False if __name__ == ‘__main__‘: if send_mail(mailto_list,"hello"): print "发送成功" else: print "发送失败"
本文出自 “向往|那一片天空” 博客,请务必保留此出处http://fiyaster.blog.51cto.com/6668127/1676254
原文地址:http://fiyaster.blog.51cto.com/6668127/1676254