【问题标题】:Reliable way of sending email of a file containing HTML code发送包含 HTML 代码的文件的电子邮件的可靠方式
【发布时间】:2013-08-28 03:42:59
【问题描述】:

我在文本文件 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()

【问题讨论】:

  • 可靠是什么意思?
  • 你试过用smtplib.SMTP而不是直接调用sendmail吗?
  • @ShivanRaptor - 格式有时会变得混乱,所以想知道是否有更好的方法来发送包含 HTML 代码的文本文件的电子邮件
  • @user2639990 你确定不只是你的 HTML 搞砸了吗?
  • @korylprince - 请提供有关如何使用 smtplib.SMTP 的意见?

标签: python html-email


【解决方案1】:

尝试使用 smtplib:

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

如果这不起作用,那么您的 HTML 可能是错误的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 2017-09-04
    • 2016-11-23
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-05
    相关资源
    最近更新 更多