|
@@ -1,5 +1,3 @@
|
|
|
-#!/usr/bin/env python
|
|
|
|
|
-#
|
|
|
|
|
# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
|
|
# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
|
|
|
#
|
|
#
|
|
|
# Permission to use, copy, modify, and distribute this software and its
|
|
# Permission to use, copy, modify, and distribute this software and its
|
|
@@ -23,9 +21,12 @@
|
|
|
of SMTPHandler.
|
|
of SMTPHandler.
|
|
|
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
|
|
Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
|
|
|
"""
|
|
"""
|
|
|
|
|
+
|
|
|
|
|
+from email.mime.text import MIMEText
|
|
|
import logging.handlers
|
|
import logging.handlers
|
|
|
import smtplib
|
|
import smtplib
|
|
|
|
|
|
|
|
|
|
+
|
|
|
class BufferingSMTPHandler(logging.handlers.BufferingHandler):
|
|
class BufferingSMTPHandler(logging.handlers.BufferingHandler):
|
|
|
def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
|
|
def __init__(self, mailhost, fromaddr, toaddrs, subject, capacity):
|
|
|
logging.handlers.BufferingHandler.__init__(self, capacity)
|
|
logging.handlers.BufferingHandler.__init__(self, capacity)
|
|
@@ -34,18 +35,22 @@ class BufferingSMTPHandler(logging.handlers.BufferingHandler):
|
|
|
self.fromaddr = fromaddr
|
|
self.fromaddr = fromaddr
|
|
|
self.toaddrs = toaddrs
|
|
self.toaddrs = toaddrs
|
|
|
self.subject = subject
|
|
self.subject = subject
|
|
|
- self.setFormatter(logging.Formatter("%(asctime)s %(levelname)-5s %(message)s"))
|
|
|
|
|
|
|
|
|
|
def flush(self):
|
|
def flush(self):
|
|
|
- if len(self.buffer) > 0:
|
|
|
|
|
- port = self.mailport if self.mailport else smtplib.SMTP_PORT
|
|
|
|
|
|
|
+ try:
|
|
|
|
|
+ if len(self.buffer) > 0:
|
|
|
|
|
+ port = self.mailport if self.mailport else smtplib.SMTP_PORT
|
|
|
|
|
|
|
|
- smtp = smtplib.SMTP(self.mailhost, port)
|
|
|
|
|
- msg = "From: {}\nTo: {}\nSubject: {}\n\n".format(self.fromaddr, ",".join(self.toaddrs), self.subject)
|
|
|
|
|
- for record in self.buffer:
|
|
|
|
|
- msg += self.format(record)
|
|
|
|
|
- msg += "\n"
|
|
|
|
|
|
|
+ msg = "\n".join([self.format(record) for record in self.buffer])
|
|
|
|
|
+ msg = MIMEText(msg.encode('utf-8'), _charset='utf-8')
|
|
|
|
|
+ msg['Subject'] = self.subject
|
|
|
|
|
+ msg['From'] = self.fromaddr
|
|
|
|
|
+ msg['To'] = ",".join(self.toaddrs)
|
|
|
|
|
|
|
|
- smtp.sendmail(self.fromaddr, self.toaddrs, msg)
|
|
|
|
|
- smtp.quit()
|
|
|
|
|
- self.buffer = []
|
|
|
|
|
|
|
+ smtp = smtplib.SMTP(self.mailhost, port)
|
|
|
|
|
+ smtp.sendmail(self.fromaddr, self.toaddrs, msg.as_string())
|
|
|
|
|
+ smtp.quit()
|
|
|
|
|
+ self.buffer = []
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ print(e)
|
|
|
|
|
+ raise
|