使用googleapp engine sendmail api创建自定义django.utils.log.AdminEmailHandler

时间:2015-10-09 05:02:48

标签: python django google-app-engine sendmail

嗨我试图覆盖django.utils.log.AdminEmailHandler类,以便使用google app engine mail api发送邮件

from google.appengine.api import mail

from django.utils.log import AdminEmailHandler

class CustomEmailHandler(AdminEmailHandler):
   def send_mail(self, subject, message, *args, **kwargs):
        logging.critical('inside sendmail')
        mailer = mail.EmailMessage(sender="xyz@gmail.com",
                                   subject=subject)
        mailer.to = "xyz@gmail.com"
        mailer.body = """Please let us know if you have any questions.
        The example.com Team    """
        mailer.send()
根据django docs

和记录配置是:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
        'mail_admins': {
            'level': 'DEBUG',
            'class': 'myapp.email_handler.CustomEmailHandler',
        },

    },

    'loggers': {
        'django.request': {
            'handlers': ['mail_admins' ],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

admin元组以

的形式给出
ADMINS=(('xyz','xyz@gmail.com'),)

1 个答案:

答案 0 :(得分:0)

得到解决方案我没有在视图调用中引发异常。我的回复状态为500 return Response(response, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

但是在向视图调用添加raise Exception('mesg')时这还不够,代码工作正常并且邮件被触发