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()

本体は次のように印刷されます。is..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 として解釈させたいとします。メッセージに content-type を設定します。

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

それ以外の場合、MUA はそれがtext/plainであると想定し、そのようにレンダリングします。

于 2013-01-01T06:35:09.003 に答える