Bussiere
Bussiere

Reputation: 1154

Receive django error debug report by email :

Here is my configuration in django settings :

MAILER_LIST = ['[email protected]']

EMAIL_HOST = 'toto.smtp.com'

EMAIL_HOST_USER = '[email protected]'

EMAIL_HOST_PASSWORD = 'tata'

EMAIL_PORT = 587

EMAIL_USE_TLS = True

DEFAULT_FROM_EMAIL = '[email protected]'

LOGGING = {

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

}

i've try to debug with :

from django.core.mail import EmailMessage
email = EmailMessage('Hello', 'World', to=['[email protected]'])
email.send()

And i get the test email if i put this in my settings.

i would like to receive this error report by email (it's just an example and i've added this error in my code to test the mail report) :

enter image description here

What am i missing to get the debug log by email ? The test is sending the email so it's not an email configuration problem ...

I would like to get the report by email and still show the debug page on django. And get the email event if debug is true or Not.

So i've set DEBUG = True in my settings.

Thanks and regards

Upvotes: 4

Views: 2302

Answers (4)

Sergio Morstabilini
Sergio Morstabilini

Reputation: 2055

Other than what has already been said in other answers, do not forget to set SERVER_EMAIL in your settings (docs).

It's the email address that error messages come from; it's similar to DEFAULT_FROM_EMAIL but SERVER_EMAIL is used only for error messages. Default value is 'root@localhost' and if you are using a provider like sendgrid your emails will be blocked.

Upvotes: 1

Yevhenii M.
Yevhenii M.

Reputation: 827

As said in another answers if you want use django build-in AdminEmailHandler, then you need provide ADMINS and MANAGERS instead of MAILER_LIST in your settings.py. Like this:

ADMINS = ['[email protected]']  # better to use another mail than EMAIL_HOST_USER
MANAGERS = ADMINS

Django's utils.log have two options for processing your DEBUG value: RequireDebugFalse and RequireDebugTrue.

So if you want send error emails to your admins (ADMINS variable in settings.py) while debug, then you may use similar settings:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue'  # log while DEBUG=True
        }
    },
    'handlers': {
        'debug_mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'filters': [require_debug_true],
        }
    },
    'loggers': {
         'django.request': {
             'handlers': ['debug_mail_admins'],
             'level': 'ERROR',
             'propagate': True,
        },
    }
}

Upd.:

Also you can use logging.handlers.SMTPHandler. Then you can write something similar to this code: https://code.djangoproject.com/ticket/15917

Upvotes: 5

joshlsullivan
joshlsullivan

Reputation: 1500

You should use ADMINS:

ADMINS = ['[email protected]']

A list of all the people who get code error notifications. When DEBUG=False and AdminEmailHandler is configured in LOGGING (done by default), Django emails these people the details of exceptions raised in the request/response cycle.

More info here

Upvotes: 0

Django handles this for you, you can add

MANAGERS = ['[email protected]']

or look into integrating sentry for a more robust error reporting, sentry is free too

Upvotes: 0

Related Questions