使用Python发送HTML格式的邮件

来源:转载

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

#"-*- coding: utf-8 -*-"import smtplibfrom email.mime.text import MIMETextmail_user="[email protected]"mail_password="XXX"mailto_list=["[email protected]"]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!"

分享给朋友:
您可能感兴趣的文章:
随机阅读: