Created
September 24, 2009 07:35
-
-
Save vsajip/192587 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging.handlers | |
import smtplib | |
class BufferingSMTPHandler(logging.handlers.BufferingHandler): | |
def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity): | |
logging.handlers.BufferingHandler.__init__(self, capacity) | |
self.mailhost = mailhost | |
self.mailport = None | |
self.fromaddr = fromaddr | |
if isinstance(toaddrs, (str, unicode)): | |
toaddrs = [toaddrs] | |
self.toaddrs = toaddrs | |
self.subject = subject | |
self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s")) | |
def flush(self): | |
if len(self.buffer) > 0: | |
try: | |
port = self.mailport | |
if not port: | |
port = smtplib.SMTP_PORT | |
smtp = smtplib.SMTP(self.mailhost, port) | |
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (self.fromaddr, ','.join(to_addrs), self.subject) | |
for record in self.buffer: | |
s = self.format(record) | |
msg = msg + s + "\r\n" | |
smtp.sendmail(self.fromaddr, self.toaddrs, msg) | |
smtp.quit() | |
except: | |
self.handleError(None) # no particular record | |
self.buffer = [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment