0

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

1 回答 1

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 可能很糟糕。

于 2013-08-28T14:07:38.627 回答