mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 14 Nov 2009 06:01:06 +0000
changeset 2 731c43b63c9d
parent 1 bab77be235a8
child 3 7062c52d3999
permissions -rw-r--r--
mailtb.py: made send_header() more flexible, added http_status dict moved mail_error_file variable to mail_config['error_log']

#!/usr/bin/env python
# -*- coding: UTF-8  -*-
# Copyright 2009 Pascal Volk
# See COPYING for distribution information.

__author__ = 'Pascal Volk'
__version__ = '0.1'
__date__ = '2009-11-14'

"""
Sends information about uncaught exceptions per email.

"""

import sys
import smtplib
import traceback
from os import environ as env
from time import ctime, gmtime, localtime, strftime, time

mail_config = {# sender information
               'from_addr': '',
               'from_name': '',
               # smtp auth information (if necessary, else leave blank)
               'auth_user': '',
               'auth_pass': '',
               # recipient information
               'rcpt_addr': '',
               'rcpt_name': '',
               # smtp server information
               'smtp_host': 'localhost',
               'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete)
               'smtp_tls':  False, # True or False (use starttls on port 25/587)
               # subject of the email
               'subject': 'Exception Notification',
               # make sure that the web server can write to this file
               'error_log': '/tmp/mailtb_error.log'
              }

http_status = {200: 'OK',
               500: 'Internal Server Error'}

def log_mail_error(error, timestamp):
    try:
        prefix = strftime('%b %d %H:%M:%S', localtime(timestamp))
        line = error.message if len(error.message) else error.args
        logfile = open(mail_config['error_log'], 'a')
        logfile.write('%s %s: %s\n' % (prefix, error.__class__.__name__, line))
        logfile.close()
    except:
        pass

def send_header(status=500, xhtml=False):
    s = status if status in http_status else 500
    write = sys.stdout.write
    if not xhtml:
        write('Content-Type: text/html; charset=utf-8\r\n')
    else:
        write('Content-Type: application/xhtml+xml; charset=utf-8\r\n')
    write('Status: %d %s\r\n\r\n' % (s, http_status[s]))
    sys.stdout.flush()

def send_error_page():
    send_header(500, True)
    print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
  <head>
    <title>Internal Server Error</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
    <h1>Internal Server Error</h1>
    <p>So wie es aussieht, ging bei der Bearbeitung Ihrer Anfrage etwas
    schief.</p>
    <p>Sofern Sie den Fehler verursacht haben, dürfen Sie jetzt ein schlechtes
    Gewissen haben.<br />
    Die Administration wird sich bei nächster Gelegenheit um das Problem
    kümmern. Bitte haben Sie dafür etwas Verständnis.</p>
    <h2>Error 500</h2>
    <p>Sorry</p>
  </body>
</html>"""

def send_mail(info):
    global mail_config
    mail_config['now'] = time()
    mail_config['host'] = info['host'] if info['host'] != 'n/a' else 'localhost'
    mail_config['date'] = strftime('%a, %d %b %Y %H:%M:%S +0000',
            gmtime(mail_config['now']))
    body = """Hi,

an uncaught exception has occurred. The details are as follows:

  Type:    %(extype)s
  Message: %(message)s

Traceback (most recent call last):
%(traceback)s

Additional information:
  Date:     %(date)s
  Request:  %(method)s %(uri)s %(protocol)s
  Referrer: %(referer)s
  Client:   %(addr)s/%(port)s
  U-Agent:  %(agent)s
""" % info
    header = """Date: %(date)s
From: "%(from_name)s" <%(from_addr)s>
To: "%(rcpt_name)s" <%(rcpt_addr)s>
Subject: %(subject)s
Message-ID: <%(now)f.%(now)X@mailtb.%(host)s>
Auto-Submitted: auto-generated
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
""" % mail_config
    
    try:
        if mail_config['smtp_port'] != 465:
            smtp = smtplib.SMTP(mail_config['smtp_host'],
                    mail_config['smtp_port'])
            if mail_config['smtp_tls']:
                smtp.starttls()
        else:
            smtp = smtplib.SMTP_SSL(mail_config['smtp_host'],
                    mail_config['smtp_port'])
        if len(mail_config['auth_user']) and len(mail_config['auth_pass']):
            smtp.login(mail_config['auth_user'], mail_config['auth_pass'])
        smtp.sendmail(mail_config['from_addr'], mail_config['rcpt_addr'],
                '\n'.join((header, body)))
    except Exception, e:
        # try to log it (fire and forget)
        log_mail_error(e, mail_config['now'])
    finally:
        smtp.quit()

def mailtbhook(extype, exval, extb):
    info = {}
    if type(extype) is type:
        info['extype'] = extype.__name__
    else:
        info['extype'] = str(extype).split("'")[1]
    if hasattr(exval, 'message'):
        info['message'] = exval.message if len(exval.message) else 'n/a'
    else:
        info['message'] = str(exval)
    info['traceback'] = ''.join(['%s\n' % l for l in traceback.format_tb(extb)])
    
    info['date'] = ctime()
    for k in ('HTTP_HOST', 'HTTP_REFERER', 'HTTP_USER_AGENT', 'REMOTE_ADDR',
             'REMOTE_PORT', 'REQUEST_METHOD', 'REQUEST_URI', 'SERVER_PROTOCOL'):
        info[k.split('_')[-1].lower()] = env[k] if env.has_key(k) else 'n/a'

    send_mail(info)
    send_error_page()

def enable():
    sys.excepthook = mailtbhook