-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_mailer.py
More file actions
89 lines (82 loc) · 3.02 KB
/
code_mailer.py
File metadata and controls
89 lines (82 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# coding: utf-8
import smtplib
import re
import sys
import traceback
import logging
def setup_logging_to_file(filename):
logging.basicConfig( filename='./'+filename,
filemode='w',
level=logging.DEBUG,
format= '%(asctime)s - %(levelname)s - %(message)s',
)
def extract_function_name():
"""Extracts failing function name from Traceback
by Alex Martelli
http://stackoverflow.com/questions/2380073/\
how-to-identify-what-function-call-raise-an-exception-in-python
"""
tb = sys.exc_info()[-1]
stk = traceback.extract_tb(tb, 1)
fname = stk[0][3]
return fname
def log_exception(e):
logging.error(
"Function {function_name} raised {exception_class} ({exception_docstring}): {exception_message}".format(
function_name = extract_function_name(), #this is optional
exception_class = e.__class__,
exception_docstring = e.__doc__,
exception_message = e.message))
def headless(inputfile):
''' Parse the list of inputs given in the specified file. (Modified from evn_funcs.py)'''
INPUTFILE = open(inputfile, "r")
control = {}
# a few useful regular expressions
newline = re.compile(r'\n')
space = re.compile(r'\s')
char = re.compile(r'\w')
comment = re.compile(r'#.*')
# parse the input file assuming '=' is used to separate names from values
for line in INPUTFILE:
if char.match(line):
line = comment.sub(r'', line)
line = line.replace("'", '')
(param, value) = line.split('=')
param = newline.sub(r'', param)
param = param.strip()
param = space.sub(r'', param)
value = newline.sub(r'', value)
value = value.replace(' ','').strip()
valuelist = value.split(',')
if len(valuelist) == 1:
if valuelist[0] == '0' or valuelist[0]=='1' or valuelist[0]=='2':
control[param] = int(valuelist[0])
else:
control[param] = str(valuelist[0])
else:
control[param] = ','.join(valuelist)
return control
def gmail_emailer(user, pwd, recipient, subject, body):
try:
import smtplib
gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(gmail_user, gmail_pwd)
server.sendmail(FROM, TO, message)
server.close()
print('Successfully sent the mail to %s' % recipient)
except:
print("Failed to send mail to %s" % recipient)
except ImportError:
print('Failed to send mail: no smtplib installed')