80

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 Answers22

118

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.example solved the problem. In settings.py:

SERVER_EMAIL = 'django@my-domain.example'
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
geekQ
  • 29,027
  • 11
  • 62
  • 58
  • 2
    Another hint that this is likely the problem is if you check your mail log and see an entry containing `sender non-delivery notification`. – jathanism Jun 25 '12 at 16:58
  • In my case the SERVER_EMAIL was in the right domain but did not exist on the email server. Changing it to an existing email solved the problem. – Sergey Orshanskiy Oct 16 '21 at 04:49
44

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.example')
)

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.example'),
)

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).

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
wxgeorge
  • 463
  • 5
  • 9
  • 1
    (It was thanks to @cathal 's answer above running a debugging SMTP server locally that allowed me to locate this as my problem). – wxgeorge Aug 08 '13 at 21:31
37

I had the same situation. I created a new project and app and it worked, so I knew it was my code. I tracked it down to the LOGGING dictionary in settings.py. I had made some changes a few weeks back for logging with Sentry, but for some reason the error just started today. I changed back to the original and got it working:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

Then, I made some changes slowly and got it working with Sentry and emailing the ADMINS as well.

Additionally, the LOGGING configuration gets merged with DEFAULT_LOGGING by default, so it's useful to have a look at the source code of django.utils.log.DEFAULT_LOGGING to understand what else may have an effect on your particular situation.

Furbeenator
  • 8,106
  • 4
  • 46
  • 54
18

Make sure your EMAIL_HOST and EMAIL_PORT are set up right in settings.py (these refer to your SMTP server). It might be assuming that you have an SMTP server running on localhost.

To test this locally, run Python's built-in test SMTP server:

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

Then set these values in your settings.py

EMAIL_HOST='localhost'
EMAIL_PORT=1025

Trigger a 500 error, and you should see the e-mail appear in the python smtpd terminal window.

Cathal
  • 286
  • 1
  • 4
  • 2
    I see the message, but if I set it back to my email settings it doesn't work –  Sep 12 '09 at 05:43
  • also added this, works well, but still nothing changes when set back to normal settings – Harry Sep 10 '15 at 13:11
8

My web hosting provider - Webfaction - only allows emails to be sent From an email that has been explicitly created in the administrator panel. Creating one fixed the problem.

  • 1
    I use webfaction and send e-mails from googlemail, so I don't think that was really the problem. – Dominic Rodger Sep 12 '09 at 06:47
  • 1
    it obviously allows you to send emails if you're using google's smtp server, but if you use smtp.webfaction.com as the host then it won't let you unless the email exists. I didn't change anything else and it fixed it so I'm pretty sure that was it. –  Sep 12 '09 at 07:49
  • This fixed the problem for me, too. I am using the telus smpt server and just switched it to authenticated as opposed to not. It worked before the change and to make it work now I had to use an e-mail address that exists to send from. – jenniwren May 01 '17 at 23:43
6

Another thing worth noting here is that settings handler500 might bypass the mechanism that sends errors on a 500 if the response from the view doesn't have a status code of 500. If you have a handler500 set, then in that view respond with something like this.

t = loader.get_template('500.html')
response = HttpResponseServerError(
    t.render(RequestContext(request, {'custom_context_var': 
        'IT BROKE OMG FIRE EVERYONE'})))
response.status_code = 500
return response
oliverseal
  • 660
  • 8
  • 15
4

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.

Miquel
  • 858
  • 11
  • 20
  • Actually.. yea. I had errors from crawlers trying to make an AJAX request without form data. I overestimated the intelligence of spam filters and ended up with all email sent by Django being caught by the spam filter. – Mike S May 28 '14 at 17:36
3

Try this

# ./manage shell
>>> from django.core.mail import send_mail
>>> send_mail('Subject here', 'Here is the message.', 'from@example.com',['to@example.com'], fail_silently=False)

With a to@example.com that you actually get email at.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Paul Tarjan
  • 48,968
  • 59
  • 172
  • 213
3

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

3

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

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Click2Death
  • 736
  • 1
  • 5
  • 18
2

Make sure you have DEBUG = False

Tim Babych
  • 3,356
  • 1
  • 20
  • 13
2

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.
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
jvandeven
  • 21
  • 1
  • 1
    Missing `SERVER_EMAIL` fixed this for me, but it is not mentioned in the official docs... – Filip Sep 24 '20 at 19:19
1

... and then there's the facepalm error, when you've used this in development to prevent emails from going out, and then accidentally copy the setting to production:

# Print emails to console
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

(of course you don't see them being printed to console when using a wsgi server). Removing the setting from production fixed this for me.

shacker
  • 14,712
  • 8
  • 89
  • 89
1

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.

djvg
  • 11,722
  • 5
  • 72
  • 103
1

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")
)
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
SuperNova
  • 25,512
  • 7
  • 93
  • 64
1

Although it's been a while, here's my response, so that other people can benefit in the future.

In my case, what was preventing emails to be sent to the ADMINS list, when an error occured, was an application specific setting. I was using django-piston, which provides the setting attributes PISTON_EMAIL_ERRORS and PISTON_DISPLAY_ERRORS. Setting these accordingly, enabled the application server to notify my by mail, whenever piston would crash.

0

While likely not ideal, I have found using Gmail as the SMTP host works just fine. There is a useful guide at nathanostgard.com.

Feel free to post your relevant settings.py sections (including EMAIL_*, SERVER_EMAIL, ADMINS (just take out your real email), MANAGERS, and DEBUG) if you want an extra set of eyes to check for typos!

John Paulett
  • 15,596
  • 4
  • 45
  • 38
0

For what it's worth I had this issue and none of these suggestions worked for me. It turns out that my problem was that SERVER_EMAIL was set to an address that the server (Webfaction) didn't recognise. If this site were hosted on Webfaction (as my other sites are), this wouldn't be a problem, but as this was on a different server, the Webfaction servers not only check the authentication of the email being sent, but also the From: value as well.

Daniel Quinn
  • 6,010
  • 6
  • 38
  • 61
0

In my case, it's the include_html in mail_admins.

When I set include_html to True,the email server reject to send my email because it think that my emails are spam.

Everything works just fine when I set include_html to False.

shellbye
  • 4,620
  • 4
  • 32
  • 44
0

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.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
SuperNova
  • 25,512
  • 7
  • 93
  • 64
0

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.

gab
  • 792
  • 1
  • 10
  • 36
IMCSAM
  • 32
  • 4
0

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.

john mba
  • 9
  • 3