使用Python发送HTML格式的邮件

来源:互联网 发布:sql 两个字段不相等 编辑:程序博客网 时间:2024/05/17 04:09

Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。具体代码如下:

#"-*- coding: utf-8 -*-"import smtplibfrom email.mime.text import MIMETextmail_user="13137082108@163.com"mail_password="XXX"mailto_list=["1820201926@qq.com"]mail_host="smtp.163.com"mail_postfix="163.com"def sendmail(to_list,sub,content):    me="徐书奎"+"<"+mail_user+"@"+mail_postfix+">"    msg=MIMEText("<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>","html","utf-8")    msg['Subject']=sub    msg['From']=me    msg['To']=",".join(to_list)    try:        server=smtplib.SMTP()        server.connect(mail_host)        server.login(mail_user,mail_password)        server.sendmail(me,to_list,msg.as_string())        server.close()        return True    except Exception,e:        print str(e)        return Falseif sendmail(mailto_list,"标题:发送的是html格式","<a href='http://www.cnblogs.com/xiaowuyi'>小五义</a>"):    print "done!"else:    print "falsed!"
原创粉丝点击