使用Python发送HTML电子邮件
使用Python发送文本消息时,所有内容均被视为简单文本。即使您在文本消息中包含HTML标签,它也将显示为简单文本,并且HTML标签不会根据HTML语法进行格式化。但是Python提供了将HTML消息作为实际HTML消息发送的选项。
在发送电子邮件时,您可以指定Mime版本,内容类型和字符集来发送HTML电子邮件。
示例
以下是将HTML内容作为电子邮件发送的示例。尝试一次-
#!/usr/bin/python
import smtplib
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特