Django - Using a different email backend for admin error emails

醉酒当歌 提交于 2019-12-07 01:46:50

问题


I'm using a custom email backend in my Django application (CeleryEmailBackend in this case):

EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'

My logging configuration:

LOGGING = {
    # ...
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
    },
    # ...
}

The Admin error emails also get sent by the same email backend.
So if there is a problem with the email backend (e.g. Celery is not running). Then I won't receive the server error emails.

Is there a way to make AdminEmailHandler use a custom email backend?


回答1:


It's possible, but in django 1.6, quote from documentation:

By setting the email_backend argument of AdminEmailHandler, the email backend that is being used by the handler can be overridden, like this:

'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        'class': 'django.utils.log.AdminEmailHandler',
        'email_backend': 'django.core.mail.backends.filebased.EmailBackend',
    }
},

If you don't want to upgrade (since 1.6 is not stable, for example), consider making a custom email handler based on AdminEmailHandler. Should not be hard, because the actual implementation of this new feature is pretty straight-forward and clean (see pull-request).

Or, you can actually extract the whole AdminEmailHandler class from the django 1.6 and use it as a custom email handler.



来源:https://stackoverflow.com/questions/19001681/django-using-a-different-email-backend-for-admin-error-emails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!