Commit 23b58585 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

add better mailing

parent ec8a30ab
......@@ -34,7 +34,6 @@ def __exit_handler(logger, notify):
elif 'DEBUG' in notify and notify['DEBUG']:
addresses = list(set(notify['DEBUG']))'Sending full DEBUG email to addresses: %s', ', '.join(addresses))
logger.email_log(addresses, level=logging.DEBUG)
else:'No errors found. Email not sent')
......@@ -141,25 +140,16 @@ class OperationalLogger:
"""Send email to addresses with current buffered errors."""
if from_address is None:
from_address = self.__from_address"Sending log email to %s", addresses)"Emailing log to: %s", ', '.join(addresses))
subject_prefix = "ERROR"
if level == logging.CRITICAL:
buf = self.__critical_buffer.getvalue()
except UnicodeDecodeError:
buf = self.__critical_buffer.getvalue().decode('utf8')
buf = self.__critical_buffer.getvalue()
subject_prefix = "CRITICAL"
elif level == logging.DEBUG:
buf = self.__debug_buffer.getvalue()
except UnicodeDecodeError:
buf = self.__debug_buffer.getvalue().decode('utf8')
buf = self.__debug_buffer.getvalue()
subject_prefix = "debug"
buf = self.__error_buffer.getvalue()
except UnicodeDecodeError:
buf = self.__error_buffer.getvalue().decode('utf8')
buf = self.__error_buffer.getvalue()
message = "<br />\n".join([self.__class__.__colorize(line) for line in buf.split('\n')])
OperationalMail.send_status(subject='[{}] Log for {}:{}'.format(subject_prefix, self.__server_name,
......@@ -5,19 +5,22 @@
import os
import smtplib
import logging
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_status(subject, message, to_list, from_address='', reply_to='',
password=None, text_attachment=None, use_monospaced=True, smtp_server='',
def send_status(subject, message, to_list, from_address='', reply_to=None,
password=None, text_attachment=None, attachments=None,
use_monospaced=True, smtp_server='',
"""Send an email message to a given recipient list."""
mail = MIMEMultipart('mixed')
mail['From'] = from_address
mail['Subject'] = subject
mail['To'] = ', '.join(to_list)
if reply_to != from_address:
if reply_to is not None and reply_to != from_address:
mail['Reply-To'] = reply_to
for key, value in kwargs.iteritems():
......@@ -40,13 +43,25 @@ def send_status(subject, message, to_list, from_address='', repl
if text_attachment:
attachment = None
text_content = None
if os.path.exists(text_attachment):
attachment = MIMEText(file(text_attachment).read())
text_content = MIMEText(file(text_attachment).read())
attachment = MIMEText(text_attachment)
attachment.add_header('Content-Disposition', 'attachment', filename='full_log.txt')
text_content = MIMEText(text_attachment)
text_content.add_header('Content-Disposition', 'attachment', filename='full_log.txt')
if attachments is not None:
for filename in attachments:
attachment = None
if os.path.exists(filename):
with open(filename, "rb") as f:
attachment = MIMEApplication(,
Content_Disposition='attachment; filename="%s"' % basename(filename),
server = smtplib.SMTP(smtp_server)
if password:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment