Skip to content

Commit 13fa722

Browse files
Chapter 17: Email notification of application errors (17b)
1 parent 0350b06 commit 13fa722

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ class ProductionConfig(Config):
4242
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
4343
'sqlite:///' + os.path.join(basedir, 'data.sqlite')
4444

45+
@classmethod
46+
def init_app(cls, app):
47+
Config.init_app(app)
48+
49+
# email errors to the administrators
50+
import logging
51+
from logging.handlers import SMTPHandler
52+
credentials = None
53+
secure = None
54+
if getattr(cls, 'MAIL_USERNAME', None) is not None:
55+
credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
56+
if getattr(cls, 'MAIL_USE_TLS', None):
57+
secure = ()
58+
mail_handler = SMTPHandler(
59+
mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
60+
fromaddr=cls.FLASKY_MAIL_SENDER,
61+
toaddrs=[cls.FLASKY_ADMIN],
62+
subject=cls.FLASKY_MAIL_SUBJECT_PREFIX + ' Application Error',
63+
credentials=credentials,
64+
secure=secure)
65+
mail_handler.setLevel(logging.ERROR)
66+
app.logger.addHandler(mail_handler)
67+
4568

4669
config = {
4770
'development': DevelopmentConfig,

0 commit comments

Comments
 (0)