【问题标题】:Testing Django Email for error handling reporting测试 Django 电子邮件以获取错误处理报告
【发布时间】:2018-01-27 01:57:23
【问题描述】:

我想设置一个自动邮件系统,当 Django 应用程序中出现异常时通知管理员用户。现在,我只想 喜欢测试电子邮件通知系统,并遵循了许多教程和提示 herehereherehere,此外还有一些其他网站。

我使用 Python 3.5 和 Django 1.8 的本地 Django 开发环境(不在现场生产场景中)。我在我的家庭网络上(不涉及代理等)

settings.py

ADMINS = (
    ('My Name', 'myhotmailaccount@hotmail.com'),
)
#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
MAILER_LIST = ['myhotmailaccount@hotmail.com']
EMAIL_HOST = 'smtp.live.com'
EMAIL_HOST_USER = 'myhotmailaccount@hotmail.com'
EMAIL_HOST_PASSWORD = 'myhotmail_password'
EMAIL_PORT = 465
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'noreply@hotmail.com'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
            'datefmt' : "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/logfile.log",
            'maxBytes': 1024*1024*5, #5 MB
            'backupCount': 5,
            'formatter': 'standard',
        },
        'request_handler':{
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': SITE_ROOT + "/django_request.log",
            'maxBytes': 1024*1024*5, #5 MB
            'backupCount': 2,
            'formatter': 'standard'
        },
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        '': {
            'handlers':['mail_admins', 'default'],
            'level':'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django': {
            'handlers': ['request_handler', 'default', 'mail_admins',],
            'propagate': True,
            'level': 'DEBUG',
        },
    }
}

来自 view.py

的 sn-p
from django.core.mail import send_mail
from django.core.mail import EmailMessage

def search(request): 
    '''
    other bits of code
    '''     
        send_mail("Subject goes here", "Text goes here", 'noreply@hotmail.com', ['myhotmailaccount@hotmail.com'], fail_silently=True)
        #msg = EmailMessage("Subject goes here", "Text goes here", 'noreply@hotmail.com', ['myhotmailaccount@hotmail.com'])
        #msg.send()
        #return HttpResponse('%s'%res)

问题是:[Errno 60] Operation timed out。由于某种我不太清楚的原因,电子邮件没有发送。我哪里错了?

【问题讨论】:

  • 很多 Django 中的电子邮件问题是因为邮件提供商不接受电子邮件。在这种情况下,Microsoft 可能不接受来自您无法控制的电子邮件地址 noreply@hotmail.com 的电子邮件。
  • @Alasdair:谢谢,但改变它并没有解决任何问题。相反,我看到了完全相同的错误消息。
  • 我对使用您的 hotmail 电子邮件没有任何其他建议。如果您无法使该 SMTP 服务器正常工作,另一种选择是将django-anymail 与 mailgun 之类的提供程序一起使用。其中许多提供商都有免费计划,足以供个人使用。
  • @Alasdair:我刚刚想通了......

标签: django python-3.x error-handling django-email django-errors


【解决方案1】:

我正在测试的 hotmail 帐户的设置配置错误。而不是:

EMAIL_HOST = 'smtp.live.com' 
EMAIL_PORT = 465

应该是:

EMAIL_HOST = 'smtp-mail.outlook.com'
EMAIL_PORT = 25

我把这条线改了(虽然没有区别):

send_mail("hi there", "Text goes here", settings.EMAIL_HOST_USER, ['myhotmailaddress@hotmail.com'], fail_silently=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多