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):
logger.email_log(addresses)
elif 'DEBUG' in notify and notify['DEBUG']:
addresses = list(set(notify['DEBUG']))
logging.info('Sending full DEBUG email to addresses: %s', ', '.join(addresses))
logger.email_log(addresses, level=logging.DEBUG)
else:
logging.info('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
logging.info("Sending log email to %s", addresses)
logging.info("Emailing log to: %s", ', '.join(addresses))
subject_prefix = "ERROR"
if level == logging.CRITICAL:
try:
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:
try:
buf = self.__debug_buffer.getvalue()
except UnicodeDecodeError:
buf = self.__debug_buffer.getvalue().decode('utf8')
buf = self.__debug_buffer.getvalue()
subject_prefix = "debug"
else:
try:
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,
os.path.basename(sys.argv[0])),
......
......@@ -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='no-reply@aer.com', reply_to='no-reply@aer.com',
password=None, text_attachment=None, use_monospaced=True, smtp_server='smtp.aer.com',
def send_status(subject, message, to_list, from_address='no-reply@aer.com', reply_to=None,
password=None, text_attachment=None, attachments=None,
use_monospaced=True, smtp_server='smtp.aer.com',
**kwargs):
"""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='no-reply@aer.com', repl
mail.attach(contentPart)
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())
else:
attachment = MIMEText(text_attachment)
attachment.add_header('Content-Disposition', 'attachment', filename='full_log.txt')
mail.attach(attachment)
text_content = MIMEText(text_attachment)
text_content.add_header('Content-Disposition', 'attachment', filename='full_log.txt')
mail.attach(text_content)
if attachments is not None:
for filename in attachments:
attachment = None
if os.path.exists(filename):
with open(filename, "rb") as f:
attachment = MIMEApplication(
f.read(),
Content_Disposition='attachment; filename="%s"' % basename(filename),
Name=basename(filename)
)
mail.attach(attachment)
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