Django not sending emails to admins

后端 未结 20 1117
梦如初夏
梦如初夏 2020-12-08 08:54

According to the documentation, if DEBUG is set to False and something is provided under the ADMINS setting, Django will send an email

20条回答
  •  独厮守ぢ
    2020-12-08 09:27

    This problem annoyed me sufficiently to motivate a post. I provide here the steps I took to resolve this problem (cutting a long story short):

    1. Set-up test page to fail (by re-naming test_template.html)
    2. Check email validations through views for test page in production using send_mail('Hello', 'hello, world', 'info@xyz.com', [('Name', 'name.name@xyz.com'),], fail_silently=False) where SERVER_EMAIL = 'info@xyz.com' and ADMINS = [('Name', 'name.name@xyz.com'),] in Django settings. In my case, I received the 'hello world' email, but not the Django admin email (which was a pain).
    3. Set-up a simple custom logger to report to a file on the server:
    LOGGING = {
      'version': 1,
      'disable_existing_loggers': False,
      'handlers': {
        'errors_file': {
          'level': 'ERROR',
          'class': 'logging.FileHandler',
          'filename': 'logs/debug.log',
        },
      },
      'loggers': {
        'django': {
          'handlers': ['errors_file'],
          'level': 'ERROR',
          'propagate': True,
        },
      },
    }
    

    In my case, navigating to the test page did not generate output in the debug.log file under the logs directory from my project root directory. This indicates that the logger was failing to reach an ERROR 'level'.

    1. Downgrade the threshold for reporting for the custom logger from ERROR to DEBUG. Now, navigating to the test page should deliver some detail. Inspecting this detail revealed in my case that the default 500 page was re-directed (inadvertedly) to an alternative template file called 500.html. This template file made use of a variable for caching, and as the template was not being called through a view that made the variable available in the context, the cache call failed with a missing key reference. Re-naming 500.html solved my problem.

提交回复
热议问题