0

我正在尝试使用 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 代码未解码

<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>
4

1 回答 1

2

哦...所以您希望 MUA 将内容解释为 html。在消息中设置内容类型:

msg["Content-Type"] = "text/html"

否则,MUA 将假定它是text/plain,并按原样渲染它。

于 2013-01-01T06:35:09.003 回答