def connect(DEFAULT, host, port): try: log.connects(DEFAULT, host, port) if (DEFAULT['-L']): server = smtplib.LMTP(host, port) else: server = smtplib.SMTP(host, port) except Exception as exc: log.connects(DEFAULT, host, port) log.errors(DEFAULT, exc) raise Exception(exc) server.set_debuglevel(DEFAULT['-v']) # If not old mode, send HELO if not DEFAULT['-o']: server.helo(DEFAULT['-M']) return server
def send(DEFAULT, server, to_list, message, count, host, port): try: server.sendmail(DEFAULT['-f'], to_list, message) except Exception as exc1: # If do not Abort whenever the server sends a negtive code if (DEFAULT['-A']): try: # If no retries after host send RST, print warning if DEFAULT['-C'] <= 0: error = 'WARNING: %s - Message #%s' % (exc1, count) log.errors(DEFAULT, error) print Exception(error) else: # Retry to send mail for i in range(0, DEFAULT['-C']): server.set_debuglevel(0) try: log.retries(DEFAULT, i+1, DEFAULT['-C']) server.sendmail(DEFAULT['-f'], to_list, message) break except Exception as exc3: error = 'WARNING: %s - Message #%s' % (exc3, count) log.errors(DEFAULT, error) print Exception(error) continue except Exception as exc2: # Still fail, print Warning but do not abort error = 'WARNING: %s - Message #%s' % (exc2, count) log.errors(DEFAULT, error) print Exception(error) else: # Raise error error = 'FATAL: %s' % exc1 log.errors(DEFAULT, error) disconnect(DEFAULT, server, host, port) raise Exception(error)