Django not sending emails to admins

后端 未结 20 1108
梦如初夏
梦如初夏 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:23

    Make sure you have DEBUG = False

    0 讨论(0)
  • 2020-12-08 09:24

    If, for some reason, you set DEBUG_PROPAGATE_EXCEPTIONS to True (it's False by default), email to admin will not work.

    0 讨论(0)
  • 2020-12-08 09:24

    And yet another thing that can go wrong (I'll just add it to the list, for those people that end up here despite all the great answers above):

    Our django setup used SendGrid as the smtp host and had a single admin email-address defined in the django settings. This worked fine for some time, but at some point, mails stopped arriving.

    As it turns out, the mail address ended up in the SendGrid 'Bounced' list for some unknown reason, causing emails to that address to be silently dropped forever after. Removing the address from that list, and whitelisting it, fixed the issue.

    0 讨论(0)
  • 2020-12-08 09:25

    Another possibility for error is trouble with your ADMINS setting. The following setting will cause the sending of mail to admins to fail quietly:

    ADMINS = (
      ('your name', 'me@mydomain.com')
    )
    

    What's wrong with that? Well ADMINS needs to be a tuple of tuples, so the above needs to be formatted as

    ADMINS = (
      ('your name', 'me@mydomain.com'),
    )
    

    Note the trailing comma. Without the failing comma, the 'to' address on the email will be incorrectly formatted (and then probably discarded silently by your SMTP server).

    0 讨论(0)
  • 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.
    0 讨论(0)
  • 2020-12-08 09:33

    In my case the cause was missing SERVER_EMAIL setting.

    The default for SERVER_EMAIL is root@localhost. But many of email servers including my email provider do not accept emails from such suspicious addresses. They silently drop the emails.

    Changing the sender email address to django@my-domain.com solved the problem. In settings.py:

    SERVER_EMAIL = 'django@my-domain.com'
    
    0 讨论(0)
提交回复
热议问题