如何以html格式发送电子邮件

时间:2013-08-24 04:19:24

标签: python email html-email

我有以下代码,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()

1 个答案:

答案 0 :(得分:0)

尝试以HTML格式发送文字

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

我没有对它进行过测试,但它应该有效。