-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmail.py
76 lines (60 loc) · 2.68 KB
/
mail.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
74
75
76
# Copyright William Lees
#
# This source code, and any executable file compiled or derived from it, is governed by the European Union Public License v. 1.2,
# the English version of which is available here: https://perma.cc/DK5U-NDVE
#
from flask_mail import Message
from head import db
from db.userdb import Role, User
from sqlalchemy import distinct
import time
from flask import current_app, render_template
def log_mail(message, app):
log_message = ""
log_message += "Sending Mail:\nSubject: %s\n" % message.subject
log_message += "To: %s" % ', '.join(message.recipients) + "\n"
if message.cc and len(message.cc) > 0:
log_message += "Cc: %s" % ', '.join(message.recipients) + "\n"
if app.config['MAIL_LOG_BODY']:
log_message += message.body + "\n"
if message.html is not None:
log_message += message.html + "\n"
app.logger.info(log_message)
# send mail - modelled after the same function in flask_security
# recipients list can include role names - which will be expanded to include role owners
def send_mail(subject, recipients, template, **context):
sender = current_app.config['MAIL_DEFAULT_SENDER']
# Expand role names into role owners
rec = []
role_names = db.session.query(distinct(Role.name)).all()
role_names = [el[0] for el in role_names]
for recipient in recipients:
if recipient != 'Test': # don't send mails to the Test role as everyone has it
if recipient in role_names:
role_owners = db.session.query(User.email).join(User.roles).filter(Role.name == recipient).all()
if len(role_owners) == 0:
current_app.logger.info('Empty role: %s' % recipient)
else:
role_owners = [el[0] for el in role_owners]
rec.extend(role_owners)
else:
rec.append(recipient)
# check for disabled accounts and remove if found
checked_rec = []
for recipient in rec:
active = db.session.query(User).filter_by(email = recipient).first().active
if active is None or active:
checked_rec.append(recipient)
else:
current_app.logger.info('Mail %s not sent to recipient %s - not active' % (subject, recipient))
rec = checked_rec
if len(rec) == 0:
current_app.logger.info('No recpients for mail %s' % (subject))
return
msg = Message(subject, sender=sender, recipients=rec)
ctx = ('email', template)
msg.body = render_template('%s/%s.txt' % ctx, **context)
msg.html = render_template('%s/%s.html' % ctx, **context)
mail = current_app.extensions.get('mail')
mail.send(msg)
time.sleep(3)