我正在尝试使用python发送sendmail,将尸体保存在可变的"正文"中,我试图使用以下来解密它,但是电子邮件的主体保持原样,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代码没有被解码
<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>
哦...所以您希望MUA将内容解释为HTML。在消息中设置内容类型:
msg["Content-Type"] = "text/html"
否则,MUA将假定它是text/plain
,然后将其呈现。