【问题标题】:how to send emai IN html format如何以html格式发送电子邮件
【发布时间】:2013-08-24 04:19:24
【问题描述】:

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

【问题讨论】:

    标签: python email html-email


    【解决方案1】:

    尝试以 HTML 格式发送文本

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

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

    【讨论】:

      猜你喜欢
      • 2010-11-26
      • 2014-06-22
      • 2015-08-04
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多