Django not sending emails to admins

Viewed 30810

According to the documentation, if DEBUG is set to False and something is provided under the ADMINS setting, Django will send an email whenever the code raises a 500 status code. I have the email settings filled out properly (as I can use send_mail fine) but whenever I intentionally put up erroneous code I get my 500.html template but no error email is sent. What could cause Django to not do this?

22 Answers

Sorry if it is too naive, but in my case the emails were sent but were going directly to the SPAM folder. Before trying more complicated things check your SPAM folder first.

Just had the same issue after upgraded to Django 2.1 from Django 1.11. Apparently the ADMINS sections in settings.py has a change. It takes a list of tuples now, rather than the old tuple of tuples. This fixed for me.

##### old #####
ADMINS = (
    ("Your Name", "your_email@company.example")
)

##### new #####
ADMINS = [
    ("Your Name", "your_email@company.example")
]

Re: https://docs.djangoproject.com/en/2.1/ref/settings/#admins

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.example, [('Name', 'name.name@xyz.example'),], fail_silently=False) where SERVER_EMAIL = info@xyz.example and ADMINS = [('Name', 'name.name@xyz.example'),] 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.

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.

If you are using or would want to use SendGrid, use the settings below in production.

Install the package

pip install sendgrid-django

Add these settings in settings.py(production)

DEBUG = False

EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"

SENDGRID_API_KEY = "That you generate in sendgrid account"

ADMINS = (
    ("Your Name", "your_email@company.example")
)

The below info is given in https://docs.djangoproject.com/en/2.1/howto/error-reporting/#email-reports

EMAIL_HOST = "email host"

EMAIL_HOST_USER = "Email username"

EMAIL_HOST_PASSWORD = "Email Password"

DEBUG = False

ADMINS = (
    ("Your Name", "your_email@company.example")
)

In order to send email, Django requires a few settings telling it how to connect to your mail server. At the very least, you’ll need to specify EMAIL_HOST and possibly EMAIL_HOST_USER and EMAIL_HOST_PASSWORD, though other settings may be also required depending on your mail server’s configuration. Consult the Django settings documentation for a full list of email-related settings.

I had the same problem and it turned out the mail server did not have the domain name of the email address. I was trying to send from a registered one (it was a new site for a different part of the business). I used an email address that was already valid under the old domain in SERVER_EMAIL. That resolved my issue.

The setting for server_email has a default of root@localhost but so many email carriers don't accept email from such email_servers and that's why the admin emails is not receiving the emails. This first answer from this trend helped me Sending email from DebuggingServer localhost:1025 not working Or you change your DEFAULT_FROM_EMAIL to something else.

Related