python - 需要有关电子邮件错误的帮助

标签 python django

我似乎无法通过电子邮件发送错误,它已成功记录,但没有发送电子邮件!没有回溯或任何其他错误,只是如果遇到错误代码 500,它会记录它,但不会向管理员发送任何电子邮件!

我在互联网上尝试了很多解决方案,但没有一个有效!

这是我的settings.py的一部分:

ADMINS = [('Prithvi', '<email1>')]


#Email Settings

MAILER_LIST = ['<email1>']
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = '<email2>' #EDITED
EMAIL_HOST_PASSWORD = '<password>'
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
EMAIL_USE_LOCALTIME = True
SERVER_EMAIL = '<email2>'
DEFAULT_FROM_EMAIL = '<email2>'

#Logger and Handler settings

# Python logging package
import logging

# Standard instance of a logger with __name__
stdlogger = logging.getLogger(__name__)

# Custom instance logging with explicit name
dbalogger = logging.getLogger('dba')

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'formatters': {
        'simple': {
            'format': '[%(asctime)s] %(levelname)s %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'verbose': {
            'format': '[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s',
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'development_logfile': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.FileHandler',
            'filename': r'C:/Users/Prithvi/Desktop/Prithvi/django/pd1/tmp/django_dev.log',###
            'formatter': 'verbose'
        },
        'production_logfile': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': r'C:/Users/Prithvi/Desktop/Prithvi/django/pd1/var/log/django/django_production.log',###
            'maxBytes' : 1024*1024*100, # 100MB
            'backupCount' : 5,
            'formatter': 'simple'
        },
        'dba_logfile': {
            'level': 'DEBUG',
            'filters': ['require_debug_false','require_debug_true'],
            'class': 'logging.handlers.WatchedFileHandler',###
            'filename': r'C:/Users/Prithvi/Desktop/Prithvi/django/pd1/var/log/dba/django_dba.log',
            'formatter': 'simple'
        },
    },
    'root': {
        'level': 'DEBUG',
        'handlers': ['console'],
    },  
    'loggers': {
        'coffeehouse': {
            'handlers': ['development_logfile','production_logfile'],
         },
        'dba': {
            'handlers': ['dba_logfile'],
        },
        'django': {
            'handlers': ['development_logfile','production_logfile'],
        },
        'py.warnings': {
            'handlers': ['development_logfile'],
        },
    }
}

我只是希望知道是什么原因造成的,而且官方文档中也没有太多关于此主题的信息来帮助我!

任何帮助将不胜感激!

最佳答案

settings.py 的这部分看起来不错。不久前我尝试过同样的事情,但意识到 gmail 不允许你如此轻松地发送电子邮件。检查您的 Gmail 收件箱,您一定收到了一封电子邮件,提示“尝试已被阻止”。它标志着您的应用程序“不够安全”。 您可以使用其他邮件服务提供商(例如 zoho)并使用他们的 smtp 服务器。 希望有帮助!!

'handlers': {
    'mail_admins': {
        'level': 'ERROR',
        #'filters': ['require_debug_false'],
        'class': 'django.utils.log.AdminEmailHandler'
    },
 }

 'loggers': {
'django': {
    'handlers': ['file', 'console', 'mail_admins',],
    'propagate': True,
    'level': 'DEBUG',
},

关于python - 需要有关电子邮件错误的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58179188/

相关文章:

list - 识别列表中连续重复项的最 Pythonic 方法是什么?

django - 使用 Django 的密码重置通知用户电子邮件无效

c++ - 导入C++模块,如果失败: import Python version?

python - 有没有办法在 Dash 仪表板上呈现 spaCy 的 NER 输出?

python - 导入错误: cannot import name RequestSite

带有 PostgreSQL 的 Django ORM 使用 icontains 返回错误的数据

Django unittest 运行特定测试语法

django - 在 django-registration 中传播 ?next=

python - 如何使用python在mongodb中使用变量

python - 为什么我在正则表达式模式中需要一个额外的空间才能使其正常工作?