Django - Using a different email backend for admin error emails
Asked Answered
S

2

8

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?

Scabious answered 25/9, 2013 at 9:54 Comment(0)
H
7

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.

Herriott answered 25/9, 2013 at 10:6 Comment(2)
Found it just now... Is there a some kind of workaround for 1.5?Scabious
For some reason, this solution does'nt work for 1.8 for me. Any one with a similar issue??Fronia
A
0

Here is my solution with Django 3 that works consistently.

  1. Add a custom ADMIN_EMAIL configuration in your settings.py:
ADMIN_EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ADMIN_EMAIL_HOST = 'smtp.gmail.com' # gmail example, put here your smtp server if another
ADMIN_EMAIL_PORT = 465
ADMIN_EMAIL_HOST_USER = 'your_mail_user'
ADMIN_EMAIL_HOST_PASSWORD = 'your_mail_password'
ADMIN_EMAIL_USE_TLS = False
  1. Overwrite AdminEmailHandler and use the previous ADMIN_EMAIL configuration:
class CustomAdminEmailHandler(AdminEmailHandler):

    def connection(self):
        return get_connection(
            backend=settings.ADMIN_EMAIL_BACKEND,
            host=settings.ADMIN_EMAIL_HOST,
            port=settings.ADMIN_EMAIL_PORT,
            username=settings.ADMIN_EMAIL_HOST_USER,
            password=settings.ADMIN_EMAIL_HOST_PASSWORD,
            use_tls=settings.ADMIN_EMAIL_USE_TLS,
            fail_silently=True
        )
  1. Use the CustomAdminEmailHandler in your mail_admins handler:
LOGGING = {
    # ...
    'handlers': {
        # ...
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'your.path.to.CustomAdminEmailHandler'

        },
        # ...
    },
    # ...
}

Hope this helps!

Amieva answered 29/4 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.