python - 是否可以在生成 500 时向 django 发送给管理员的电子邮件添加 header ?

标签 python django email

我正在使用第三方发送电子邮件,他们允许我通过向通过他们发送的电子邮件添加标题来对我的电子邮件进行分类。

是否可以在异常(exception)电子邮件发送前添加标题?或者至少,我将通过捕获中间件中的异常来管理发送电子邮件,但是如何生成 django 在 500 上发送给我的良好电子邮件响应?

编辑:我知道如何向电子邮件添加 header ,我知道如何通过中间件处理异常。我对如何生成 Django 在异常时发送的相同电子邮件感兴趣,以便我可以添加 header 。

最佳答案

  1. 子类 AdminEmailHandler(在 django.utils.log 中定义)。
  2. 配置logging相应地。

AdminEmailHandler 的工作原理如下:

class AdminEmailHandler(logging.Handler):
    """An exception log handler that emails log entries to site admins.

    If the request is passed as the first argument to the log record,
    request data will be provided in the email report.
    """

    def __init__(self, include_html=False):
        logging.Handler.__init__(self)
        self.include_html = include_html

    def emit(self, record):
        try:
            request = record.request
            subject = '%s (%s IP): %s' % (
                record.levelname,
                (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS and 'internal' or 'EXTERNAL'),
                record.msg
            )
            filter = get_exception_reporter_filter(request)
            request_repr = filter.get_request_repr(request)
        except:
            subject = '%s: %s' % (
                record.levelname,
                record.getMessage()
            )
            request = None
            request_repr = "Request repr() unavailable."

        if record.exc_info:
            exc_info = record.exc_info
            stack_trace = '\n'.join(traceback.format_exception(*record.exc_info))
        else:
            exc_info = (None, record.getMessage(), None)
            stack_trace = 'No stack trace available'

        message = "%s\n\n%s" % (stack_trace, request_repr)
        reporter = ExceptionReporter(request, is_email=True, *exc_info)
        html_message = self.include_html and reporter.get_traceback_html() or None
        mail.mail_admins(subject, message, fail_silently=True, html_message=html_message)

仅供引用:我之前的回答。

  1. 创建自定义中间件:从 https://code.djangoproject.com/browser/django/trunk/django/middleware/common.py 中的 CommonMiddleware 中汲取灵感(看看 process_response)
  2. 根据https://code.djangoproject.com/browser/django/trunk/django/core/mail/message.py 创建函数send_mail_with_exception_header

这是一个例子:

# Custom middleware

class MyErrorMiddleware(object):
    def process_response(self, request, response):
        if response.status_code == 404:
            domain = request.get_host()
            referer = request.META.get('HTTP_REFERER', None)
            is_internal = _is_internal_request(domain, referer)
            path = request.get_full_path()
                if referer and not _is_ignorable_404(path) and (is_internal or '?' not in referer):
                    ua = request.META.get('HTTP_USER_AGENT', '<none>')
                    ip = request.META.get('REMOTE_ADDR', '<none>')
                    mail_error("Broken %slink on %s" % ((is_internal and 'INTERNAL ' or ''), domain),
                        "Referrer: %s\nRequested URL: %s\nUser agent: %s\nIP address: %s\n" \
                                  % (referer, request.get_full_path(), ua, ip),
                                  fail_silently=True)
                return response

# Custom mail_error function

def mail_error(subject, message, fail_silently=False, connection=None,
                  html_message=None):
    """Sends a message to the managers, as defined by the MANAGERS setting."""
    if not settings.MANAGERS:
        return

    # put your extra headers here
    mail = EmailMultiAlternatives(u'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
                message, settings.SERVER_EMAIL, [a[1] for a in settings.MANAGERS],
                connection=connection, header={})
    mail
    if html_message:
        mail.attach_alternative(html_message, 'text/html')
    mail.send(fail_silently=fail_silently)

关于python - 是否可以在生成 500 时向 django 发送给管理员的电子邮件添加 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8752765/

相关文章:

python - 将用户输入传递给节日

c# - EWS : Retrieving attachments from signed emails

php - 服务器端推送的可扩展解决方案?

django 测试客户端失败,但 url 有效

html - Drupal webform html 邮件

Django : Convert UTC to local time zone in 'Views'

python - Tango 与 Django 第 6 章 - URL 不起作用

python - 使用临时表中的大量数据填充表 - MySQL

python - 在 Google Colab/Jupyter Notebook 中进行条件 pip 安装

python - Pandas 加入 2 列