0

我有以下代码,它在 gerrit.txt 中的文本中发送电子邮件...gerrit.txt 的内容是 HTML 代码,位于http://pastie.org/8264638,问题是在发送电子邮件时(在 Outlook 中)它通过电子邮件发送 HTML内容...如何以 HTML 格式发送电子邮件?

from email.mime.text import MIMEText
from subprocess import check_call,Popen,PIPE

def email (body,subject,to=None):
      msg = MIMEText("%s" % body)
      msg['Content-Type'] = "text/html;"
      msg["From"] = "you@yoursite.com"
      if to!=None:
          to=to.strip()
          msg["To"] = to
      else:
          msg["To"] = "user@domain.com"
      msg["Subject"] = '%s' % subject
      p = Popen(["/usr/sbin/sendmail", "-t", "-f" + msg["From"]], stdin=PIPE)
      (stddata, errdata) = p.communicate(input="To: " + msg["To"] + "\r\nFrom: " + msg["From"] + "\r\nSubject: " + subject + "\r\nImportance: Normal\r\n\r\n" + body)
      print stddata, errdata
      print "Done"


def main ():
    # open gerrit.txt and read the content into body
    with open('gerrit.txt', 'r') as f:
        body = f.read()

    Subject ="test email"
    email(body, Subject, "from@domain.com")

if __name__ == '__main__':
    main()
4

1 回答 1

0

尝试以 HTML 格式发送文本

  /*"<html><body>Email Body text<\body><\html>"*/

我还没有测试过,但它应该可以工作。

于 2013-08-24T04:44:32.780 回答