forked from ticketfrei/ticketfrei
attach logfiles to shutdown mails
This commit is contained in:
parent
e962bbbe85
commit
01ad0e1c40
|
@ -76,7 +76,7 @@ class Logger(object):
|
||||||
self.log(logmessage)
|
self.log(logmessage)
|
||||||
try:
|
try:
|
||||||
mailer = sendmail.Mailer(self.config)
|
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:
|
except:
|
||||||
self.log("Error while shutdown: " + self.generate_tb(sys.exc_info()))
|
self.log("Error while shutdown: " + self.generate_tb(sys.exc_info()))
|
||||||
print()
|
print()
|
||||||
|
|
23
sendmail.py
23
sendmail.py
|
@ -3,6 +3,8 @@
|
||||||
import smtplib
|
import smtplib
|
||||||
import pytoml as toml
|
import pytoml as toml
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from email.mime.application import MIMEApplication
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
|
||||||
|
|
||||||
class Mailer(object):
|
class Mailer(object):
|
||||||
|
@ -12,6 +14,9 @@ class Mailer(object):
|
||||||
|
|
||||||
def __init__(self, config):
|
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
|
:param config: The config file generated from config.toml
|
||||||
"""
|
"""
|
||||||
|
@ -25,21 +30,35 @@ class Mailer(object):
|
||||||
self.s.starttls()
|
self.s.starttls()
|
||||||
self.s.login(config["mail"]["user"], config["mail"]["passphrase"])
|
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 text: (string) the content of the mail
|
||||||
:param recipient: (string) the recipient of the mail
|
:param recipient: (string) the recipient of the mail
|
||||||
:param subject: (string) the subject 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
|
:return: string for logging purposes, contains recipient & subject
|
||||||
"""
|
"""
|
||||||
msg = MIMEText(text)
|
msg = MIMEMultipart()
|
||||||
|
msg.attach(MIMEText(text))
|
||||||
|
|
||||||
msg["From"] = self.fromaddr
|
msg["From"] = self.fromaddr
|
||||||
msg["To"] = recipient
|
msg["To"] = recipient
|
||||||
msg["Subject"] = subject
|
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.send_message(msg)
|
||||||
|
self.s.close()
|
||||||
|
|
||||||
return "Sent mail to " + recipient + ": " + subject
|
return "Sent mail to " + recipient + ": " + subject
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue