我正在尝试使用python发送邮件,在变量“body”中保存正文我尝试使用下面解密它但是电子邮件的正文保持原样,html代码没有得到解码..我查看了其他帖子在stackoverflow上,但无法获得任何实质性的......哪里出错了?
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
def email (body,subject):
msg = MIMEText("%s" % body)
msg["From"] = "test@company.com"
msg["To"] = "bot@qualcomm.com"
msg["Subject"] = 'The contents of %s' % subject
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_string())
def main ():
subject="Test subject"
body = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
email(body,subject)
if __name__ == '__main__':
main()
正在按照以下方式打印正文...原始代码未被解码
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
答案 0 :(得分:2)
哦......所以你希望MUA将内容解释为html。在消息中设置内容类型:
msg["Content-Type"] = "text/html"
否则,MUA将假设它为text/plain
,并将其渲染为此类。