From 5af7fc5eb30586079b7b52e6e42f9e5268af1063 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 18 Jul 2017 07:55:53 -0700 Subject: [PATCH] Chapter 17: Email notification of application errors (17b) --- config.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/config.py b/config.py index 4c8bf6b56..dbe76968e 100644 --- a/config.py +++ b/config.py @@ -42,6 +42,29 @@ class ProductionConfig(Config): SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ 'sqlite:///' + os.path.join(basedir, 'data.sqlite') + @classmethod + def init_app(cls, app): + Config.init_app(app) + + # email errors to the administrators + import logging + from logging.handlers import SMTPHandler + credentials = None + secure = None + if getattr(cls, 'MAIL_USERNAME', None) is not None: + credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD) + if getattr(cls, 'MAIL_USE_TLS', None): + secure = () + mail_handler = SMTPHandler( + mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT), + fromaddr=cls.FLASKY_MAIL_SENDER, + toaddrs=[cls.FLASKY_ADMIN], + subject=cls.FLASKY_MAIL_SUBJECT_PREFIX + ' Application Error', + credentials=credentials, + secure=secure) + mail_handler.setLevel(logging.ERROR) + app.logger.addHandler(mail_handler) + config = { 'development': DevelopmentConfig,