簡單的python發送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 = "[email protected]" 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) #添加多個郵箱的時候以逗號隔開 sendmaillist=sendmail.split(",") msg['To'] = ";".join(sendmaillist) #html #讀取html文件 html = open('test.html').read() #實例化爲html部分,並設置編碼 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 = "[email protected],[email protected]" sub = "測試標題" Mail(maillist,sub)