我可以使用以下代码发送邮件
E:\Python\django-test\LYYDownloaderServer>python manage.py shell
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>>
>>> send_mail(
... 'Subject here',
... 'Here is the message.',
... 'redstone-cold@163.com',
... ['2281570025@qq.com'],
... fail_silently=False,
... )
1
>>>
根据doc:
当DEBUG为False时,Django将通过电子邮件向ADMINS中列出的用户发送电子邮件 每当您的代码引发未处理的异常和结果时进行设置 在内部服务器错误(HTTP状态代码500)。这给了 管理员立即通知任何错误。 ADMINS将 获取错误的描述,完整的Python回溯,以及 有关导致错误的HTTP请求的详细信息。
但在我的情况下,Django不会通过电子邮件报告内部服务器错误(HTTP状态代码500)
有什么问题?请帮助解决问题
settings.py
"""
Django settings for LYYDownloaderServer project.
Generated by 'django-admin startproject' using Django 1.9.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
ADMINS = [('Philip', 'r234327894@163.com'), ('Philip2', '768799875@qq.com')]
EMAIL_HOST = 'smtp.163.com' # 'localhost'#'smtp.139.com'
# EMAIL_PORT = 25
# EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'r234327894@163.com' # '13529123633@139.com'
EMAIL_HOST_PASSWORD = '******'
# DEFAULT_FROM_EMAIL = 'r234327894@163.com'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's4(z8qzt$=x(2t(ok5bb58_!u==+x97t0vpa=*8bb_68baekkh'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']#, '.0letter.com'
# Application definition
INSTALLED_APPS = [
'VideoParser.apps.VideoparserConfig',
'FileHost.apps.FilehostConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE_CLASSES = [
'django.middleware.common.BrokenLinkEmailsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'LYYDownloaderServer.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'LYYDownloaderServer.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.9/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
views.py
的开头from django.http import JsonResponse, HttpResponse
import logging
import m3u8
import os
from VideoParser.parsers.CommonParsers import *
import urllib.parse
import hashlib
from datetime import datetime, timedelta, date
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from VideoParser.models import *
from importlib import import_module
# print('-------------views --------')
FILES_DIR = 'files'
# specialHostName2module = {'56': 'v56'}
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d %I:%M:%S %p', level=logging.ERROR, handlers=[logging.handlers.RotatingFileHandler(filename=os.path.join(FILES_DIR, 'LYYDownloaderServer.log'), maxBytes=1024 * 1024, backupCount=1)])
...
答案 0 :(得分:2)
首先,如果您能够使用控制台发送邮件,但是如果您收到邮件,则无关紧要。我猜你做到了。
其次,最好在控制台中尝试与ADMINS
中设置的电子邮件地址完全相同的电子邮件地址,以确定。
最后,发件人地址也可能很重要。默认值为" root @ localhost",而" root"没关系," localhost"不是,一些邮件服务器可能会拒绝该电子邮件。通过设置SERVER_EMAIL
Django设置指定其他发件人电子邮件地址。
答案 1 :(得分:0)
Djano使用日志系统发送错误的管理员电子邮件。
正如我从views.py
中看到的那样,您正在更改日志记录设置。
当您清除django管理员mail_admins
时,这可能是问题的原因。
有关详细信息,请查看django documentation
答案 2 :(得分:0)
Django doc说:为了发送电子邮件,至少需要EMAIL_HOST,EMAIL_HOST_USER和EMAIL_HOST_PASSWORD,但正如我测试的那样,我们还应该指定SERVER_EMAIL,并且只有当SERVER_EMAIL等于EMAIL_HOST_USER时才可以发送电子邮件,例如
EMAIL_HOST = 'smtp.163.com'
SERVER_EMAIL = '234327894-cold@163.com' #
EMAIL_HOST_USER = '234327894-cold@163.com' #
EMAIL_HOST_PASSWORD = '234327894123' #
Django使用AdminEmailHandler向网站管理员发送电子邮件,以收到它收到的每条日志消息。
除了Django,我们也可以使用Python
logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0)
做同样的事。
例如,将以下代码放入views.py
(更改为您的帐户),它将通过电子邮件报告未处理的异常并导致内部服务器错误(HTTP状态代码500)。
import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d %I:%M:%S %p', handlers=[logging.handlers.SMTPHandler('smtp.163.com', '234327894-cold@163.com', ['234327894-cold@163.com'], 'LYYDownloader Server Exception', credentials=('234327894-cold@163.com', '234327894123'))])
比较AdminEmailHandler
和SMTPHandler
,我主张尽可能使用SMTPHandler
。
首先,AdminEmailHandler
是Django特有的,而你可以在任何Python程序中使用SMTPHandler
,你应该关心的一件事是在客户端软件中使用SMTPHandler
时,一些防病毒软件可能会对待该软件是间谍软件,因此您应该在软件即将发送电子邮件时通知用户。
其次,我发现AdminEmailHandler
发送的电子邮件有一堆信息,而SMTPHandler
只发送Python异常信息,这使得调试有点清晰!
第三,如果你在Django的settings.py
内配置你的电子邮件,即使你确认电子邮件确认有问题也没有例外,而SMTPHandler
总是抛出异常错误在使用 。
来自http://redstoneleo.blogspot.com/2016/12/email-reporting-exceptions-and-errors_30.html