Django没有给管理员发送电子邮件

时间:2022-11-15 13:09:17

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?

根据文档,如果调试被设置为False,并且在ADMINS设置下提供了一些内容,那么每当代码生成500个状态代码时,Django就会发送电子邮件。我已经正确地填写了电子邮件设置(因为我可以使用send_mail fine),但是每当我故意设置错误的代码时,我就会得到我的500。html模板,但没有发送错误邮件。什么会导致Django不这样做呢?

16 个解决方案

#1


86  

In my case the cause was missing SERVER_EMAIL setting.

在我的例子中,原因是缺少SERVER_EMAIL设置。

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.

SERVER_EMAIL的默认值是root@localhost。但是,包括我的电子邮件提供商在内的许多电子邮件服务器都不接受来自这些可疑地址的电子邮件。他们默默地放下电子邮件。

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

将发件人邮箱地址改为django@my-domain.com解决了这个问题。在settings.py:

SERVER_EMAIL = 'django@my-domain.com'

#2


37  

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

注意逗号。如果没有失败的逗号,电子邮件中的“to”地址将被错误地格式化(然后可能被SMTP服务器悄无声息地丢弃)。

#3


31  

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:

我也有同样的情况。我创建了一个新的项目和应用程序,它成功了,所以我知道这是我的代码。我在settings.py中找到了日志字典。几个星期前,我对哨兵日志做了一些更改,但是由于某些原因,错误今天才开始。我换回原来的版本,让它正常工作:

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.

此外,默认情况下,日志配置与DEFAULT_LOGGING合并,因此查看一下django.utils.log的源代码是很有用的。使用DEFAULT_LOGGING来了解还可能对您的特定情况产生什么影响。

#4


14  

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.

确保您的EMAIL_HOST和EMAIL_PORT在设置中设置正确。py(这些指向SMTP服务器)。它可能假设您有一个运行在本地主机上的SMTP服务器。

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

要在本地测试这一点,请运行Python的内置测试SMTP服务器:

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

Then set these values in your settings.py

然后在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.

触发500个错误,您应该会在python smtpd终端窗口中看到电子邮件。

#5


6  

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.

我的虚拟主机提供商——web派别——只允许从管理员面板中显式创建的电子邮件发送电子邮件。创建一个解决了这个问题。

#6


2  

Make sure you have DEBUG = False

确保您有DEBUG = False

#7


2  

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.

如果太天真的话,我很抱歉,但在我的情况下,这些邮件是被发送的,但是是直接发送到垃圾邮件文件夹的。在尝试更复杂的事情之前,先检查你的垃圾邮件文件夹。

#8


2  

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.

这里值得注意的另一件事是,如果来自视图的响应没有500的状态码,设置handler500可能会绕过在500上发送错误的机制。如果你有一个handler500集合,那么在那个视图中,你的响应是这样的。

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

#9


1  

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.

你可以在to@example.com上收到电子邮件。

#10


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.

在我的例子中,当发生错误时,阻止向ADMINS列表发送电子邮件的是应用程序特定的设置。我使用的是django活塞,它提供了设置属性PISTON_EMAIL_ERRORS和PISTON_DISPLAY_ERRORS。相应地设置这些,使应用服务器能够在活塞崩溃时通过邮件通知my。

#11


1  

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

如果由于某种原因,您将debug_propagation _exception设置为True(默认为False),则发送给admin的电子邮件将不会工作。

#12


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:

…然后是facepalm错误,当你在开发中使用它来防止电子邮件离开时,然后不小心将设置复制到生产中:

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

(当然,在使用wsgi服务器时,您不会看到它们被打印到控制台)。从生产中删除这个设置为我解决了这个问题。

#13


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.

虽然可能不太理想,但我发现使用Gmail作为SMTP主机很好。在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!

请随意发布相关设置。py部分(包括EMAIL_*、SERVER_EMAIL、ADMINS(只需拿出您的真实电子邮件)、管理器和调试),如果您希望有一组额外的眼睛来检查输入错误!

#14


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.

不管怎么说,我有这个问题,这些建议对我都不起作用。结果发现,我的问题是,SERVER_EMAIL被设置为服务器无法识别的地址。如果这个站点是在weberge(和我的其他站点一样)上托管的,那么这就不是问题了,但是由于这个站点是在另一个服务器上,web179服务器不仅检查发送的电子邮件的身份验证,而且还检查From: value。

#15


0  

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

在我的例子中,它是mail_admins中的include_html。

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

当我将include_html设置为True时,邮件服务器拒绝发送我的邮件,因为它认为我的邮件是垃圾邮件。

Everything works just fine when I set include_html to False.

当我将include_html设置为False时,一切都很正常。

#16


0  

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.

我们的django设置使用SendGrid作为smtp主机,并且在django设置中定义了一个管理电子邮件地址。这种做法在一段时间内很有效,但在某些时候,邮件停止了。

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.

事实证明,由于某些未知的原因,邮件地址最终出现在SendGrid ' bounce '列表中,导致该地址的电子邮件在此后一直被静静地删除。从列表中删除地址并将其白化,修复了这个问题。

#1


86  

In my case the cause was missing SERVER_EMAIL setting.

在我的例子中,原因是缺少SERVER_EMAIL设置。

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.

SERVER_EMAIL的默认值是root@localhost。但是,包括我的电子邮件提供商在内的许多电子邮件服务器都不接受来自这些可疑地址的电子邮件。他们默默地放下电子邮件。

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

将发件人邮箱地址改为django@my-domain.com解决了这个问题。在settings.py:

SERVER_EMAIL = 'django@my-domain.com'

#2


37  

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

注意逗号。如果没有失败的逗号,电子邮件中的“to”地址将被错误地格式化(然后可能被SMTP服务器悄无声息地丢弃)。

#3


31  

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:

我也有同样的情况。我创建了一个新的项目和应用程序,它成功了,所以我知道这是我的代码。我在settings.py中找到了日志字典。几个星期前,我对哨兵日志做了一些更改,但是由于某些原因,错误今天才开始。我换回原来的版本,让它正常工作:

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.

此外,默认情况下,日志配置与DEFAULT_LOGGING合并,因此查看一下django.utils.log的源代码是很有用的。使用DEFAULT_LOGGING来了解还可能对您的特定情况产生什么影响。

#4


14  

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.

确保您的EMAIL_HOST和EMAIL_PORT在设置中设置正确。py(这些指向SMTP服务器)。它可能假设您有一个运行在本地主机上的SMTP服务器。

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

要在本地测试这一点,请运行Python的内置测试SMTP服务器:

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

Then set these values in your settings.py

然后在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.

触发500个错误,您应该会在python smtpd终端窗口中看到电子邮件。

#5


6  

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.

我的虚拟主机提供商——web派别——只允许从管理员面板中显式创建的电子邮件发送电子邮件。创建一个解决了这个问题。

#6


2  

Make sure you have DEBUG = False

确保您有DEBUG = False

#7


2  

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.

如果太天真的话,我很抱歉,但在我的情况下,这些邮件是被发送的,但是是直接发送到垃圾邮件文件夹的。在尝试更复杂的事情之前,先检查你的垃圾邮件文件夹。

#8


2  

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.

这里值得注意的另一件事是,如果来自视图的响应没有500的状态码,设置handler500可能会绕过在500上发送错误的机制。如果你有一个handler500集合,那么在那个视图中,你的响应是这样的。

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

#9


1  

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.

你可以在to@example.com上收到电子邮件。

#10


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.

在我的例子中,当发生错误时,阻止向ADMINS列表发送电子邮件的是应用程序特定的设置。我使用的是django活塞,它提供了设置属性PISTON_EMAIL_ERRORS和PISTON_DISPLAY_ERRORS。相应地设置这些,使应用服务器能够在活塞崩溃时通过邮件通知my。

#11


1  

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

如果由于某种原因,您将debug_propagation _exception设置为True(默认为False),则发送给admin的电子邮件将不会工作。

#12


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:

…然后是facepalm错误,当你在开发中使用它来防止电子邮件离开时,然后不小心将设置复制到生产中:

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

(当然,在使用wsgi服务器时,您不会看到它们被打印到控制台)。从生产中删除这个设置为我解决了这个问题。

#13


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.

虽然可能不太理想,但我发现使用Gmail作为SMTP主机很好。在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!

请随意发布相关设置。py部分(包括EMAIL_*、SERVER_EMAIL、ADMINS(只需拿出您的真实电子邮件)、管理器和调试),如果您希望有一组额外的眼睛来检查输入错误!

#14


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.

不管怎么说,我有这个问题,这些建议对我都不起作用。结果发现,我的问题是,SERVER_EMAIL被设置为服务器无法识别的地址。如果这个站点是在weberge(和我的其他站点一样)上托管的,那么这就不是问题了,但是由于这个站点是在另一个服务器上,web179服务器不仅检查发送的电子邮件的身份验证,而且还检查From: value。

#15


0  

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

在我的例子中,它是mail_admins中的include_html。

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

当我将include_html设置为True时,邮件服务器拒绝发送我的邮件,因为它认为我的邮件是垃圾邮件。

Everything works just fine when I set include_html to False.

当我将include_html设置为False时,一切都很正常。

#16


0  

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.

我们的django设置使用SendGrid作为smtp主机,并且在django设置中定义了一个管理电子邮件地址。这种做法在一段时间内很有效,但在某些时候,邮件停止了。

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.

事实证明,由于某些未知的原因,邮件地址最终出现在SendGrid ' bounce '列表中,导致该地址的电子邮件在此后一直被静静地删除。从列表中删除地址并将其白化,修复了这个问题。