【问题标题】:Python executing sendmail but not sending emailPython 执行 sendmail 但不发送电子邮件
【发布时间】:2014-08-04 16:39:54
【问题描述】:

我检查了 Stackoverflow 并发现了类似的问题,但似乎没有一个能够回答我的具体问题。我正在使用 Python 2.6。

以下函数用于通过 sendmail 成功发送带有附件的纯文本电子邮件:

def sndmsg(u,A,a):
    """
    + sends email(s) with plain text and attachment

    u = str - company url
    A = str - manager's name
    a = list of email addresses to send to
    """
    u=u.replace('/','')
    file = open('message.txt','rb')
    txt=file.read()
    file.close()
    for t in a:
        out = MIMEMultipart()
        out['Subject'] = 'Free Report'
        out['From'] = 'admin@example.com'
        out['To'] = t.strip()
        out['Date'] = date()

        pt1 = MIMEText(txt.format(A,u))
        out.attach(pt1)

        att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
        att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
        out.attach(att)

        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(out.as_string())

随后对其进行了修改以包含 html 格式的文本(如下)。代码执行良好,并毫无错误地进入下一个函数,但这次我的任何收件箱或垃圾邮件文件夹中都没有收到电子邮件。

def sndmsg(u,A,a):
    """
    + sends email(s) with html text, plain text and attachment

    u = str - company url
    A = str - manager's name
    a = list of email addresses to send to

    H[0] = str - html table
    """
    u=u.replace('/','')
    file = open('message.txt','rb')
    txt=file.read()
    file.close()
    file = open('message.html','rb')
    htm=file.read()
    file.close()
    for t in a:
        out = MIMEMultipart()
        out['Subject'] = 'Free Report'
        out['From'] = 'admin@example.com'
        out['To'] = t.strip()
        out['Date'] = date()

        inn = MIMEMultipart('alternative')
        pt1 = MIMEText(txt.format(A,u))
        pt2 = MIMEText(htm.format(A,u,H[0]), 'html')
        inn.attach(pt1)
        inn.attach(pt2)
        out.attach(inn)

        att = MIMEApplication(open('/path/'+u+'.xls',"rb").read())
        att.add_header('Content-Disposition', 'attachment', filename=u+'.xls')
        out.attach(att)

        p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
        p.communicate(out.as_string())

我的主要问题是它是编码问题还是Sendmail 问题。

我尝试独立使用第二个功能,并在浏览器中单独打开了 html 消息,看起来不错,但电子邮件仍然没有发送。

我没有对SSH 进入服务器的root 访问权限并检查/var/log/mail。我已经询问了服务器管理员,但没有得到任何帮助。

如果是编码错误,需要修复或改进什么?

感谢您对我的无知的耐心和您的帮助。

【问题讨论】:

  • 好吧,一方面,H 是未定义的。
  • 感谢您的评论。是的,H 是一个全局变量,其中包含在其他函数中调用以生成 html 表的 str 列表,其中一个 H[0] 被插入到此模板电子邮件中。

标签: python email sendmail


【解决方案1】:
  1. -i 添加到 sendmail 的命令行选项

    p = Popen(["/usr/sbin/sendmail", "-t", "-i"], stdin=PIPE)

  2. 显式关闭 Popen 创建的管道

    rc = p.close() if rc is not None and rc >> 8: print "There were some errors"

  3. 如果无法修复,则将-v(详细/调试)选项添加到 sendmail 的命令行选项并在终端中执行您的 stipt 以查看其 stderr 输出。


https://docs.python.org/2/library/subprocess.html#popen-objects

【讨论】:

  • 非常感谢!另外,感谢您提供的文档。知道在哪里看真的很有帮助。我真的应该先看看那里,但我不确定要找什么。现在我明白了。再次感谢你。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-03
  • 2016-12-31
  • 2017-01-20
  • 2018-07-22
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
相关资源
最近更新 更多