
import logging.handlers
import smtplib
from email.mime.text import MIMEText

class GmailHandler(logging.Handler):
	""" sends a gmail email with the logging message as the subject """
	def emit(self, record):
		try:
			message = MIMEText('') # no body text
			message['Subject'] = self.format(record)
			smtp = smtplib.SMTP('smtp.gmail.com', 587)
			smtp.ehlo() 
			smtp.starttls() 
			smtp.ehlo() 
			addr = 'username@gmail.com'
			smtp.login("username@gmail.com", "password")
			smtp.sendmail(addr, [addr,], message.as_string())
			smtp.quit()
		except (KeyboardInterrupt, SystemExit):
			raise
		except:
			self.handleError(record)
