开发者

Erratic behavior with email attachments using python smtplib

开发者 https://www.devze.com 2023-02-01 22:32 出处:网络
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.

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(['thinkwelldesigns@g1234.com'], cd.contact, 'dave@1234.com', 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(['thinkwelldesigns@1234.com'], cd.contact, 'dave@1234.com', 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, messa开发者_如何学Goge.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


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.


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. :-|

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号