python发html邮件_python 发送html邮件
簡單的python發(fā)送html郵件代碼,如下:#!/usr/bin/env python
#-*- coding:utf-8 -*-
import smtplib
from email.header import Header
from email.MIMEText import MIMEText
from email.mime.multipart import MIMEMultipart
########################################################################
MailHost = "mail.xxxx.com"
MailUser = "user@xxxx.com"
MailPswd = "password"
MailPostfix = "xxxx.com"
########################################################################
def Mail(sendmail,sub):
Me = MailUser
msg = MIMEMultipart()
#組裝信頭
msg['Subject'] = Header(sub,'utf-8')
#使用國際化編碼
msg['From'] = r"%s <%s>" % (Header("測試郵件","utf-8"),Me)
#添加多個(gè)郵箱的時(shí)候以逗號(hào)隔開
sendmaillist=sendmail.split(",")
msg['To'] = ";".join(sendmaillist)
#html
#讀取html文件
html = open('test.html').read()
#實(shí)例化為html部分,并設(shè)置編碼
html_part = MIMEText(html,'html','utf-8')
#綁定到message里
msg.attach(html_part)
#send mail
try:
s = smtplib.SMTP()
s.connect(MailHost)
s.login(MailUser, MailPswd)
s.sendmail(Me, sendmaillist, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == "__main__":
maillist = "test1@qq.com,test2@163.com"
sub = "測試標(biāo)題"
Mail(maillist,sub)
總結(jié)
以上是生活随笔為你收集整理的python发html邮件_python 发送html邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果 watchOS 10 开发者预览版
- 下一篇: 长城汽车起诉六个涉嫌严重侵权账号 &ld