使用 python smtplib 的电子邮件附件行为不稳定

发布于 2024-10-09 08:45:33 字数 2855 浏览 0 评论 0原文

我的电子邮件脚本基于 Fine青蛙。我使用此脚本从使用各种 ISP 的多个远程计算机发送 HTML 日志文件。

然而,附件的发送并不一致。它确实在 80% 的情况下有效,但我遇到了两种奇怪的行为。第一个是根本不发送电子邮件,而是抛出您在下面看到的错误

Traceback (most recent call last):


File "/root/sapapps/reporter/usage_report.py", line 336, in ?
    se.send_mail(['[email protected]'], cd.contact, '[email protected]', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
  File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
    mail_server.sendmail(server_addr, addresses, message.as_string())
  File "/usr/lib/python2.4/smtplib.py", line 692, in sendmail
    (code,resp) = self.data(msg)
  File "/usr/lib/python2.4/smtplib.py", line 489, in data
    self.send(q)
  File "/usr/lib/python2.4/smtplib.py", line 319, in send
    raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected

,或者,在某些计算机上,会引发此错误。

    reply: '421 Command timeout, closing transmission channel\r\n'
reply: retcode (421); Msg: Command timeout, closing transmission channel
data: (421, 'Command timeout, closing transmission channel')
send: 'rset\r\n'
Traceback (most recent call last):
  File "/root/sapapps/reporter/usage_report.py", line 336, in ?
    se.send_mail(['[email protected]'], cd.contact, '[email protected]', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
  File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
    mail_server.sendmail(server_addr, addresses, message.as_string())
  File "/usr/lib/python2.4/smtplib.py", line 694, in sendmail
    self.rset()
  File "/usr/lib/python2.4/smtplib.py", line 449, in rset
    return self.docmd("rset")
  File "/usr/lib/python2.4/smtplib.py", line 374, in docmd
    return self.getreply()
  File "/usr/lib/python2.4/smtplib.py", line 348, in getreply
    line = self.file.readline()
  File "/usr/lib/python2.4/socket.py", line 340, in readline
    data = self._sock.recv(self._rbufsize)
socket.error: (104, 'Connection reset by peer')

在第二个奇怪行为的实例中,电子邮件的处理没有错误,附件也继续存在,但在电子邮件发送过程中,一些数据从 HTML 附件中被删除。 IOW,如果您登录到远程计算机,您会发现原始文件完好无损,但附加文件丢失了日志条目。

我猜这些问题是不相关的问题,但是有人对如何最可靠地发送 HTML 附件有一些建议吗?

蒂亚,

戴夫

My email script is based on this script at Fine Frog. I'm using this script to send HTML log files from a number of remote machines using a variety of ISPs.

The attachment isn't being sent consistently, however. It does work 80% of the time, but I'm getting two types of weird behavior. The first is where the email isn't sent at all, but throws the error(s) you see below

Traceback (most recent call last):


File "/root/sapapps/reporter/usage_report.py", line 336, in ?
    se.send_mail(['[email protected]'], cd.contact, '[email protected]', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
  File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
    mail_server.sendmail(server_addr, addresses, message.as_string())
  File "/usr/lib/python2.4/smtplib.py", line 692, in sendmail
    (code,resp) = self.data(msg)
  File "/usr/lib/python2.4/smtplib.py", line 489, in data
    self.send(q)
  File "/usr/lib/python2.4/smtplib.py", line 319, in send
    raise SMTPServerDisconnected('Server not connected')
smtplib.SMTPServerDisconnected: Server not connected

Or, on some machines, this error is raised.

    reply: '421 Command timeout, closing transmission channel\r\n'
reply: retcode (421); Msg: Command timeout, closing transmission channel
data: (421, 'Command timeout, closing transmission channel')
send: 'rset\r\n'
Traceback (most recent call last):
  File "/root/sapapps/reporter/usage_report.py", line 336, in ?
    se.send_mail(['[email protected]'], cd.contact, '[email protected]', report_subject, text_body, files=[report_name], bcc=[cd.tech_email])
  File "/usr/lib/python2.4/site-packages/sap/send_email.py", line 61, in send_mail
    mail_server.sendmail(server_addr, addresses, message.as_string())
  File "/usr/lib/python2.4/smtplib.py", line 694, in sendmail
    self.rset()
  File "/usr/lib/python2.4/smtplib.py", line 449, in rset
    return self.docmd("rset")
  File "/usr/lib/python2.4/smtplib.py", line 374, in docmd
    return self.getreply()
  File "/usr/lib/python2.4/smtplib.py", line 348, in getreply
    line = self.file.readline()
  File "/usr/lib/python2.4/socket.py", line 340, in readline
    data = self._sock.recv(self._rbufsize)
socket.error: (104, 'Connection reset by peer')

In the second instance of weird behavior, the email is processed without error, the attachment goes along, but some data is stripped out of the HTML attachment in the emailing process. IOW, if you login to the remote machine, you'll find the original file intact, but the attached file is missed log entries.

I'm guessing that these issues are unrelated problems, but does anyone have some advice on how to most reliably send HTML attachments?

TIA,

Dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风尘浪孓 2024-10-16 08:45:33

我在使用 smtplib.SMTP 之前遇到了问题,并找出了使用 SSL 连接所需的电子邮件服务器。如果常规调用失败,您可以尝试使用 smtplib.SMTP_SSL。

I had a problem before using smtplib.SMTP, and figured out the email server required using an SSL connection. You could try using smtplib.SMTP_SSL if the regular call fails.

故人如初 2024-10-16 08:45:33

第一个问题引发了以下两个错误之一:

smtplib.SMTPServerDisconnected: Server not connected 

或者

socket.error: (104, 'Connection reset by peer') 

通过切换到 Gmail 作为邮件服务器来解决。

第二个错误(部分 HTML 附件被删除)是通过在处理电子邮件脚本之前关闭报告文件解决的……

html_report.close()

我是 Python 新手。 :-|

The first problem which raised one of the two following errors:

smtplib.SMTPServerDisconnected: Server not connected 

or

socket.error: (104, 'Connection reset by peer') 

was solved by switching to Gmail as the mail server.

The second error where part of HTML attachment was stripped out was solved by closing the report file before the email script processed...

html_report.close()

I'm a Python newbie. :-|

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文