From 6cb30f36d6d1b29fa0b0d98624841b60533dcfd8 Mon Sep 17 00:00:00 2001 From: b3yond Date: Fri, 5 Jan 2018 11:20:07 +0100 Subject: [PATCH] attach logfiles to shutdown mails --- logger.py | 2 +- sendmail.py | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/logger.py b/logger.py index ca23e92..29c4a00 100644 --- a/logger.py +++ b/logger.py @@ -76,7 +76,7 @@ class Logger(object): self.log(logmessage) try: mailer = sendmail.Mailer(self.config) - mailer.send(tb, self.contact, "Ticketfrei Crash Report") + mailer.send(tb, self.contact, "Ticketfrei Crash Report", attachment=self.logpath) except: self.log("Error while shutdown: " + self.generate_tb(sys.exc_info())) print() diff --git a/sendmail.py b/sendmail.py index 4b58da7..0486bfd 100755 --- a/sendmail.py +++ b/sendmail.py @@ -3,6 +3,8 @@ import smtplib import pytoml as toml from email.mime.text import MIMEText +from email.mime.application import MIMEApplication +from email.mime.multipart import MIMEMultipart class Mailer(object): @@ -12,6 +14,9 @@ class Mailer(object): def __init__(self, config): """ + Creates an SMTP client to send a mail. Is called only once + when you actually want to send a mail. After you sent the + mail, the SMTP client is shut down again. :param config: The config file generated from config.toml """ @@ -25,21 +30,35 @@ class Mailer(object): self.s.starttls() self.s.login(config["mail"]["user"], config["mail"]["passphrase"]) - def send(self, text, recipient, subject): + def send(self, text, recipient, subject, attachment=None): """ :param text: (string) the content of the mail :param recipient: (string) the recipient of the mail :param subject: (string) the subject of the mail + :param attachment: (string) the path to the logfile :return: string for logging purposes, contains recipient & subject """ - msg = MIMEText(text) + msg = MIMEMultipart() + msg.attach(MIMEText(text)) msg["From"] = self.fromaddr msg["To"] = recipient msg["Subject"] = subject + # attach logfile + if attachment: + with open(attachment, "rb") as fil: + part = MIMEApplication( + fil.read(), + Name="logfile" + ) + # After the file is closed + part['Content-Disposition'] = 'attachment; filename="logfile"' + msg.attach(part) + self.s.send_message(msg) + self.s.close() return "Sent mail to " + recipient + ": " + subject