阿里狼 发表于 2017-5-8 11:48:25

如何用python发html和plain text格式的邮件

import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart

smtp_sever = smtplib.SMTP('smtp_host')
msgRoot = MIMEMultipart('related')
msg['From'] = sender
msg['To'] = recepient
msg['Subject'] = 'Hello'
msgAlernative = MIMEMultipart('alernative')
msgRoot.attach(msgAlernative)
html="<html><body>Hello world</body></html>
msgHtml = MIMEText(html, 'html')   
msgAlernative.attach(msgHtml)
text = 'Hello world'
msgTxt = MIMEText(text)
msgAlernative.attach(msgTxt)
smtp_server.sendmail(sender, recepient, msgRoot.as_string())
 
页: [1]
查看完整版本: 如何用python发html和plain text格式的邮件