Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7017497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:55:19+00:00 2026-05-27T22:55:19+00:00

I’m using a third party to send email, and they allow me to categorize

  • 0

I’m using a third party to send email, and they allow me to categorize my email by adding headers to the emails i send through them.

Is it possible to either add a header to the exception email before it gets sent? Or at least, I’ll managing sending the email by catching the exception in the middleware, but how do I generate the nice email response django sends me on a 500?

EDIT: I know how to add headers to emails, I know how to process exception through middleware. I am interested in how to generate the same email that django sends on exception, so that i can add a header.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:55:20+00:00Added an answer on May 27, 2026 at 10:55 pm
    1. Subclass AdminEmailHandler (defined in django.utils.log).
    2. Configure logging accordingly.

    Here is how AdminEmailHandler works:

    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)
    

    For reference only: my previous answer.

    1. Create a custom middleware: take inspiration from CommonMiddleware in https://code.djangoproject.com/browser/django/trunk/django/middleware/common.py (have a look at process_response)
    2. Create a function send_mail_with_exception_header based on https://code.djangoproject.com/browser/django/trunk/django/core/mail/message.py

    Here is an example:

    # 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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.