*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects

Add SMTP error handling

Merged Tsepelakis, Sotirios requested to merge st/dev into master
6 files
+ 155
23
Compare changes
  • Side-by-side
  • Inline
Files
6
+ 46
1
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
#
#
# Copyright (C) 2020 - 2021 TU Wien.
# Copyright (C) 2020-2022 TU Wien.
#
#
# Invenio-Config-TUW is free software; you can redistribute it and/or modify
# Invenio-Config-TUW is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.
# it under the terms of the MIT License; see LICENSE file for more details.
@@ -8,12 +8,16 @@
@@ -8,12 +8,16 @@
"""Invenio module containing some customizations and configuration for TU Wien."""
"""Invenio module containing some customizations and configuration for TU Wien."""
from functools import partial
from functools import partial
 
from logging.handlers import SMTPHandler
from flask_security.signals import user_registered
from flask_security.signals import user_registered
 
from invenio_mail import InvenioMail
from . import config
from . import config
from .auth.utils import auto_trust_user
from .auth.utils import auto_trust_user
 
from .formatters import CustomFormatter
from .permissions import TUWCommunitiesPermissionPolicy
from .permissions import TUWCommunitiesPermissionPolicy
 
from .utils import remove_duplicate_smtp_handlers
@user_registered.connect
@user_registered.connect
@@ -40,6 +44,13 @@ class InvenioConfigTUW(object):
@@ -40,6 +44,13 @@ class InvenioConfigTUW(object):
self.init_config(app)
self.init_config(app)
app.extensions["invenio-config-tuw"] = self
app.extensions["invenio-config-tuw"] = self
self.override_communities_permissions(app)
self.override_communities_permissions(app)
 
# Ensure that invenio_mail is registered in the app.
 
if "invenio-mail" not in app.extensions:
 
InvenioMail(app)
 
# Email error handling should occur only in production mode.
 
if not app.debug and not app.testing:
 
self.register_smtp_error_handler(app)
 
remove_duplicate_smtp_handlers(app.logger)
def init_config(self, app):
def init_config(self, app):
"""Initialize configuration."""
"""Initialize configuration."""
@@ -77,3 +88,37 @@ class InvenioConfigTUW(object):
@@ -77,3 +88,37 @@ class InvenioConfigTUW(object):
)
)
override_func = partial(self.override_communities_permissions, app)
override_func = partial(self.override_communities_permissions, app)
app.before_first_request_funcs.append(override_func)
app.before_first_request_funcs.append(override_func)
 
 
def register_smtp_error_handler(self, app):
 
"""Register email error handler to the application."""
 
# Check if mail server and admin(s) email(s) are present in the config
 
# If not raise a warning
 
if app.config.get("MAIL_SERVER") and app.config.get("MAIL_ADMIN"):
 
# Configure Auth
 
auth = None
 
if app.config.get("MAIL_USERNAME") and app.config.get("MAIL_PASSWORD"):
 
auth = (app.config["MAIL_USERNAME"], app.config["MAIL_PASSWORD"])
 
# Configure TLS
 
secure = None
 
if app.config.get("MAIL_USE_TLS"):
 
secure = ()
 
 
# Initialize SMTP Handler
 
mail_handler = SMTPHandler(
 
mailhost=(app.config["MAIL_SERVER"], app.config.get("MAIL_PORT", 25)),
 
fromaddr=app.config["SECURITY_EMAIL_SENDER"],
 
toaddrs=app.config["MAIL_ADMIN"],
 
subject=app.config["THEME_SITENAME"] + " - Failure",
 
credentials=auth,
 
secure=secure
 
)
 
# level 40 is for ERROR. See python's logging for further mappings.
 
mail_handler.setLevel(40)
 
# add custom formatter
 
mail_handler.setFormatter(CustomFormatter())
 
# Attach to the application
 
app.logger.addHandler(mail_handler)
 
else:
 
app.logger.warning(
 
"Mail configuration missing: SMTP error handler not registered!"
 
)
Loading