发送包含HTML代码的文件的电子邮件的可靠方式

时间:2013-08-28 03:43:00

标签: python html-email

我在文本文件emailtext.txt @ http://pastie.org/8276028中有一个HTML代码,目前我使用以下代码以HTML格式发送电子邮件(通过outlook),但格式似乎有时搞砸了。 ..有人可以提供有关如何以可靠的方式发送此电子邮件的输入吗?

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; charset=UTF8"
          msg["From"] = "userid@qualcomm.com"
          if to!=None:
              to=to.strip()
              msg["To"] = to
          else:
              msg["To"] = "userid2@company.com"
          msg["Subject"] = '%s' % subject
          p = Popen(["/usr/sbin/sendmail", "-t", "-f" + msg["From"]], stdin=PIPE)
          p.communicate(msg.as_string())
          print "Done"


    def main ():

        with open('emailtext.txt', 'r') as f:
            body = f.read()

        Subject ="test email"
        email(body, Subject, "userid3@company.com")

    if __name__ == '__main__':
        main()

1 个答案:

答案 0 :(得分:0)

尝试使用smtplib:

from smtplib import SMTP
s = SMTP('localhost',25)
s.sendmail('Me <me@example.com>', ['You <you@example.com>'],msg=msg.as_string())

如果这不起作用那么你的HTML可能不好。