-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail_handler.py
73 lines (68 loc) · 2.25 KB
/
mail_handler.py
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
import os
import flask
import smtplib
import logging
import functools
import threading
from email.Utils import formatdate
from email.mime.text import MIMEText
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email import Encoders
threads = []
f = open("data","r")
user_name = f.readline()[:-1]
pass_word = f.readline()
f.close()
def clean_threads(threads=threads):
ctrl = 0
for t in threads:
if t.isAlive():
ctrl += 1
if ctrl == 0:
return []
else:
return threads
def login_required(method):
@functools.wraps(method)
def wrapper(*args, **kwargs):
if 'username' in flask.session:
return method(*args, **kwargs)
else:
flask.flash(flask.Markup("You need to log in to access <em>Zq Computational</em>."))
return flask.redirect(flask.url_for("welcome"))
return wrapper
def send_mail(para, titulo, contenido, adjuntos=[]):
msg = MIMEMultipart()
msg['Subject'] = titulo
msg['From'] = user_name
msg['Date'] = formatdate(localtime=True)
msg['To'] = para
msg.attach( MIMEText(contenido) )
# Attaching the file
if len(adjuntos) != 0:
for item in adjuntos:
if item in os.listdir('.'):
logging.info('Output file found! Processing ...')
results = open(item,'r').read()
part = MIMEBase('application', "octet-stream")
part.set_payload( results )
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(item))
msg.attach(part)
else:
logging.warning('Output file not found.')
results = "The input file had errors and no output coulf be generated."
try:
logging.info('Trying to establish connection through SMTP.')
s = smtplib.SMTP('smtp.gmail.com',587)
s.ehlo()
s.starttls()
s.ehlo
s.login(user_name, pass_word)
s.sendmail(user_name, [para], msg.as_string())
s.quit()
logging.info('email sent to: ' + para)
except Exception as e:
logging.error('Connection failed! email was not sent.')
logging.error(e)