2

What's the easiest way to configure Django to send error emails to a special email account?

The docs on error emails don't explicitly mention any way to do this. I know how to write a custom email backend that could lookup and use different credentials, but as the EmailBackend._send method only receives the message, I'm not sure how to detect when the message is in response to a 500 server error.

Cerin
  • 60,957
  • 96
  • 316
  • 522

2 Answers2

4

You use the logging settings to mail 500 errors to the admin email ids.

Example of a logging setup :

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        }
    }
}

This will mail all 500 errors in django to the mentioned email ids.

See: Elegant setup of Python logging in Django

and :

https://docs.djangoproject.com/en/dev/topics/logging/#an-example

If you want to use another SMTP server, then use exception middleware .

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

in the process_exception method, you can email the traceback of exception to required email accounts.

Community
  • 1
  • 1
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • I'm not trying to just send 500 errors to a specific email address. That's clearly documented. I want to use completely different email account credentials entirely. I don't want error emails going through the same SMTP server used for normal emails. – Cerin Jun 24 '13 at 18:05
0

https://stackoverflow.com/a/19001979/483642 It is way easy than below answer. You just need inherit django.core.mail.backends.smtp.EmailBackend and set email_backend on your logging config.

Community
  • 1
  • 1
Delgermurun
  • 648
  • 9
  • 19