Last active
September 12, 2016 00:47
-
-
Save jeb2239/09fb6b508a11dc53179cd2d13e643ef4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import logging.handlers | |
class TlsSMTPHandler(logging.handlers.SMTPHandler): | |
def emit(self, record): | |
""" | |
Emit a record. | |
Format the record and send it to the specified addressees. | |
""" | |
try: | |
import smtplib | |
import string # for tls add this line | |
try: | |
from email.utils import formatdate | |
except ImportError: | |
formatdate = self.date_time | |
port = self.mailport | |
if not port: | |
port = smtplib.SMTP_PORT | |
smtp = smtplib.SMTP(self.mailhost, port) | |
msg = self.format(record) | |
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % ( | |
self.fromaddr, | |
string.join(self.toaddrs, ","), | |
self.getSubject(record), | |
formatdate(), msg) | |
if self.username: | |
smtp.ehlo() # for tls add this line | |
smtp.starttls() # for tls add this line | |
smtp.ehlo() # for tls add this line | |
smtp.login(self.username, self.password) | |
smtp.sendmail(self.fromaddr, self.toaddrs, msg) | |
smtp.quit() | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except: | |
self.handleError(record) | |
logger = logging.getLogger() | |
gm = TlsSMTPHandler(("smtp.gmail.com", 587), 'bugs@my_company.com', ['admin@my_company.com'], 'Error found!', ('my_company_account@gmail.com', 'top_secret_gmail_password')) | |
gm.setLevel(logging.ERROR) | |
logger.addHandler(gm) | |
try: | |
1/0 | |
except: | |
logger.exception('FFFFFFFFFFFFFFFFFFFFFFFUUUUUUUUUUUUUUUUUUUUUU-') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment