简体   繁体   English

间歇式SMTP断管(错误32)

[英]intermittent smtp broken pipe (errno 32)

I have a python script that sends out a few sets of emails (to different addresses with different contents) using smtplib periodically throughout the day. 我有一个python脚本,它整天定期使用smtplib发送几组电子邮件(到具有不同内容的不同地址的电子邮件)。 Somewhat frequently (say about 1 in 5 times when sending out batches of more than one email at the same time), I get a IOError (errno: broken pipe). 有点频繁(一次发送一批以上的电子邮件时,大约五分之一),我得到了IOError(错误:管道破损)。 I try reseting and quitting the SMTP server and then connect to the server again and attempt to resend, but that always fails if it failed the first time (with the same exception). 我尝试重置并退出SMTP服务器,然后再次连接到该服务器并尝试重新发送,但是如果它第一次失败(总是有同样的例外),则总是失败。 The SMTP server is maintained by the college and should be reliable (and allows loginless emails as long as you are on the intranet). SMTP服务器由大学维护,并且应该可靠(只要您在Intranet上,就可以允许无登录电子邮件)。

Ignoring the ugliness of the code below (lack of DRY), can anyone suggest a more reliable way to connect? 忽略下面代码的丑陋(缺少DRY),有人可以建议一种更可靠的连接方式吗?

I create a class called EmailSet which will send a batch of a few emails that has a member function send_emails: 我创建了一个名为EmailSet的类,该类将发送一批具有成员函数send_emails的电子邮件:

class EmailSet(object):
    ...
    def send_emails(self):
        try: # Connect to server
            server = smtplib.SMTP( smtp_server_name_str, 25)
            server.set_debuglevel(self.verbose)
            server.ehlo()
            for email in self.email_set:
                try: # send 1st mail
                    logging.debug("Sending email to %r" % email.recipients)        
                    response_dict = server.sendmail(email.fromaddr, email.recipients, email.msg_str())
                    logging.info("Sent email to %r" % email.recipients)        
                except Exception as inst:
                    logging.error('RD: %r' % response_dict)
                    logging.error("Email Sending Failed")
                    logging.error("%r %s" % ( type(inst), inst ) )
                    try: # send second mail
                        logging.info("Second Attempt to send to %r" % email.recipients)
                        try:
                            server.rset() 
                            server.quit()
                        except:
                            pass
                        time.sleep(60) # wait 60s
                        server = smtplib.SMTP( smtp_server_name_str, 25)
                        server.set_debuglevel(self.verbose)
                        server.ehlo()
                        response_dict = server.sendmail(email.fromaddr, email.recipients, email.msg_str())
                        logging.info("Sent email to %r (2nd Attempt)" % email.recipients)        
                    except Exception as inst:
                        try:
                            logging.error('RD: %r' % response_dict)
                        except:
                            pass
                        logging.error("Second Attempt Email Sending Failed")
        except:
            logging.debug("Can't connect to server")
        finally:
            logging.debug("Reseting and Quitting Server")
            server.rset()
            server.quit()
            logging.debug("Successfully Quit Server")
        return True

Any thoughts on how to proceed debugging this? 关于如何进行调试的任何想法? The stmp server isn't maintained by me, though should be well-maintained (used for ~10k person organization). 我不维护stmp服务器,尽管应该维护良好(用于约1万人组织)。 I originally connected and disconnected from the smtpserver after sending each email, but that produced more errors than this method. 发送每封电子邮件后,我最初是从smtpserver连接和断开的,但这种方法产生的错误更多。

Also would it be safer to use /usr/sbin/sendmail rather than smtplib? 使用/ usr / sbin / sendmail而不是smtplib会更安全吗?

Also would it be safer to use /usr/sbin/sendmail rather than smtplib?

From the point of view of handling a message queue and a queue-runner for retries. 从处理消息队列和重试队列运行器的角度来看。 Yes, sendmail or another local MTA could handle this for you. 是的,sendmail或其他本地MTA可以为您解决此问题。

Any thoughts on how to proceed debugging this?

Packet capture. 数据包捕获。 Use wireshark to capture the smtp traffic, see whats going on. 使用wireshark捕获SMTP流量,看看发生了什么。 You've got a lot of broad exception handling that isn't necessarily showing you the exact error. 您有很多广泛的异常处理方法,不一定能向您显示确切的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM