Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django not sending error emails - how can I debug?

Tags:

python

django

I'm using Django 1.8. This is my base settings file:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}
ADMINS = (
   ('ME', '[email protected]'),
)
MANAGERS = ADMINS

And these are my production settings:

########## EMAIL CONFIGURATION
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '[email protected]'
EMAIL_HOST_PASSWORD = utils.get_env_setting('GMAIL_PASS')
EMAIL_SUBJECT_PREFIX = '[%s] ' % SITE_NAME
SERVER_EMAIL = EMAIL_HOST_USER
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
########## END EMAIL CONFIGURATION

It used to send error email in production, but has stopped. I've set up a page that returns 500 errors so that I can test this further - no emails being sent when I load it.

I've tried debugging the obvious things:

  • I can still log into [email protected] and it doesn't seem to have been blocked.
  • I've checked my spam filter.
  • I'm certain that DEBUG is set false.
  • I believe the GMAIL_PASS environment variable is available to the Django user.

How can I debug this further?

like image 670
Richard Avatar asked Aug 13 '15 11:08

Richard


People also ask

What is settings debug in Django?

One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).

What is debug false in Django?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.


1 Answers

  • For the debugging part of the question

    @zopieux commented:

    First, check that mails are being sent. Set:

    EMAIL_HOST = 'localhost'
    EMAIL_PORT = 1025
    EMAIL_USE_TLS = False
    EMAIL_USE_SSL = False
    

    Then run a dummy SMTP server:

    python -m smtpd -n -c DebuggingServer localhost:1025

    If this works, you can revert the changes and manually send an email as described in this relevant question:

    from django.core.mail import EmailMessage
    email = EmailMessage('Hello', 'World', to=['[email protected]'])
    email.send()
    
  • For the feature of using Google as SMTP server:

    The most popular -and updated- answer of the question states that Google does not support anymore this feature (2016) and that you should try to find another SMTP server.

I have posted my working logging configuration for reference.

like image 57
raratiru Avatar answered Sep 30 '22 18:09

raratiru