4

Here is my configuration in django settings :

MAILER_LIST = ['toto@toto.com']

EMAIL_HOST = 'toto.smtp.com'

EMAIL_HOST_USER = 'toto@toto.com'

EMAIL_HOST_PASSWORD = 'tata'

EMAIL_PORT = 587

EMAIL_USE_TLS = True

DEFAULT_FROM_EMAIL = 'toto@toto.com'

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=['toto@toto.com'])
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

Bussiere
  • 500
  • 13
  • 60
  • 119
  • 1
    I think the error is pretty self-explanatory. You have referenced variable tt before assigned. In the stack trace before the error, it will show you exact file and line where it is crashing. – Harsh Nagarkar Apr 07 '20 at 14:00

4 Answers4

5

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 = ['toto@toto.com']  # 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

Yevhenii M.
  • 817
  • 1
  • 6
  • 22
  • @yevhenil M i would like to still see the debug report on the website – Bussiere Apr 11 '20 at 08:07
  • @user462794 that's why I used ```require_debug_true``` - this option allow logging with debug reports web-pages. If this doesn't work then you need write custom handler based on ```django.utils.log.AdminEmailHandler``` or better on ```logging.handlers.SMTPHandler```. – Yevhenii M. Apr 11 '20 at 17:21
  • But don't use ```DEBUG=True``` settings on production. This can lead to sensitive data leaks and impair site security. – Yevhenii M. Apr 11 '20 at 17:21
1

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.

Sergio Morstabilini
  • 2,035
  • 21
  • 28
0

Django handles this for you, you can add

MANAGERS = ['mail@mail.com']

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

0

You should use ADMINS:

ADMINS = ['notifications@example.com']

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

joshlsullivan
  • 1,375
  • 2
  • 14
  • 21