Verified Commit c4b442a2 authored by Anton Akhmerov's avatar Anton Akhmerov
Browse files

factor out email settings into app config

Closes #225
parent fcff914f
......@@ -6,3 +6,11 @@ DEBUG = 1
# Page size supported by the printers, one of 'A4' or "US letter"
PAGE_FORMAT = 'A4'
# Email settings
USE_SSL = False
SMTP_SERVER = 'dutmail.tudelft.nl'
SMTP_PORT = '25'
FROM_ADDRESS = 'noreply@tudelft.nl'
SMTP_USERNAME = ''
SMTP_PASSWORD = ''
......@@ -27,6 +27,9 @@ if 'ZESJE_SETTINGS' in os.environ:
app.config.update(
DATA_DIRECTORY=abspath(app.config.get('DATA_DIRECTORY', 'data')),
)
app.config.update(
FROM_ADDRESS=app.config.get('FROM_ADDRESS', 'no-reply@example.com'),
)
# These reference DATA_DIRECTORY, so they need to be in a separate update
app.config.update(
......
......@@ -74,6 +74,7 @@ def build_email(exam_id, student_id, template, attach, copy_to=None):
if attach
else None,
copy_to=copy_to,
email_from=app.config['FROM_ADDRESS'],
)
......@@ -158,8 +159,23 @@ class Email(Resource):
return self._send_all(exam_id, template, attach)
def _send_single(self, exam_id, student_id, template, attach, copy_to):
message = build_email(exam_id, student_id, template, attach, copy_to=copy_to)
failed = emails.send({student_id: message})
if not app.config.get('SMTP_SERVER'):
abort(
500,
message='Sending email not configured'
)
message = build_email(
exam_id, student_id, template, attach, copy_to=copy_to
)
failed = emails.send(
{student_id: message},
server=app.config['SMTP_SERVER'],
from_address=app.config['FROM_ADDRESS'],
port=app.config.get('SMTP_PORT'),
user=app.config.get('SMTP_USERNAME'),
password=app.config.get('SMTP_PASSWORD'),
use_ssl=app.config.get('USE_SSL'),
)
if failed:
abort(
500,
......@@ -168,6 +184,11 @@ class Email(Resource):
return dict(status=200)
def _send_all(self, exam_id, template, attach):
if not app.config.get('SMTP_SERVER'):
abort(
500,
message='Sending email not configured'
)
with orm.db_session:
student_ids = set(Exam[exam_id].submissions.student.id)
......@@ -182,7 +203,15 @@ class Email(Resource):
# so we let other exceptions raise.
failed_to_build.append(student_id)
failed_to_send = emails.send(to_send)
failed_to_send = emails.send(
to_send,
server=app.config['SMTP_SERVER'],
from_address=app.config['FROM_ADDRESS'],
port=app.config.get('SMTP_PORT'),
user=app.config.get('SMTP_USERNAME'),
password=app.config.get('SMTP_PASSWORD'),
use_ssl=app.config.get('USE_SSL'),
)
sent = set(student_ids) - (set(failed_to_send) | set(failed_to_build))
sent = list(sent)
......
......@@ -76,16 +76,37 @@ def build(email_to, content, attachment=None, copy_to=None,
return msg
def send(messages):
def send(
messages,
from_address,
server, port=0, user=None, password=None, use_ssl=None,
):
"""Send a dict of messages
Takes a dict where the values are the messages to send, and
the keys are unique identifiers.
Parameters
----------
messages : dictionary
A dict where the values are the messages to send, and
the keys are unique message identifiers.
server : string
SMTP server domain name or IP address.
port : int, optional
STMP port.
user, password : string, optional
Login credentials.
use_ssl : bool or None
Whether to use SSL connection. If not provided, it is inferred from the
port.
Returns a list of the identifiers for messages that failed to send.
"""
failed = []
with smtplib.SMTP('dutmail.tudelft.nl', 25) as s:
if use_ssl is None:
use_ssl = port == 465
server_type = smtplib.SMTP_SSL if use_ssl else smtplib.SMTP
with server_type(server, port) as s:
if user and password:
s.login(user, password)
for identifier, message in messages.items():
recipients = [
*message['To'].split(','),
......@@ -93,7 +114,7 @@ def send(messages):
]
try:
s.sendmail(
'noreply@tudelft.nl',
from_address,
recipients,
message.as_string()
)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment