Skip to content

Instantly share code, notes, and snippets.

@cpatdowling
Last active June 23, 2017 23:30
Show Gist options
  • Save cpatdowling/b3d239ac03d9c3e25b3a59bb4d258bf6 to your computer and use it in GitHub Desktop.
Save cpatdowling/b3d239ac03d9c3e25b3a59bb4d258bf6 to your computer and use it in GitHub Desktop.
Python 2.7+ class for sending failure notifications via gmail to oneself; can be nice emails too, I'm not your boss.
#Requires google application password generation if using 2-factor authentication
#https://support.google.com/accounts/answer/185833?hl=en
#Update credential storage method in __init__ method
#Example usage:
"""
from send_notif_email import *
try:
thing.__main__()
except Exception as err:
fail = notification("thing: main execution")
fail.send_message(str(err))
"""
import smtplib
from email.mime.text import MIMEText
class notification:
def __init__(self, app_name):
with open("credential storage", 'r') as f:
self.user = f.readline().strip()
self.pswd = f.readline().strip()
self.app_name = app_name
def send_message(self, message_string):
msg = MIMEText(message_string)
msg['Subject'] = 'Job notification: ' + self.app_name
msg['From'] = self.user
msg['To'] = self.user
serv = smtplib.SMTP(host='smtp.gmail.com', port=587)
serv.ehlo()
serv.starttls()
serv.login(self.user, self.pswd)
serv.sendmail(msg['From'], msg['To'], msg.as_string())
serv.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment