mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Fri, 06 Nov 2009 05:15:51 +0000
changeset 0 3acd8a788a6f
child 1 bab77be235a8
permissions -rw-r--r--
initial commit: »don't fear the nervous delete finger«
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     1
#!/usr/bin/env python
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     2
# -*- coding: UTF-8  -*-
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     3
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     4
"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     5
Sends information about uncaught exceptions per email.
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     6
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     7
"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     8
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     9
import sys
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    10
import smtplib
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    11
import traceback
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    12
from os import environ as env
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    13
from time import ctime, gmtime, localtime, strftime, time
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    14
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    15
# make sure that the web server can write to this file
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    16
mail_error_file = '/tmp/mailtb_error.log'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    17
mail_config = {# sender information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    18
               'from_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    19
               'from_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    20
               # smtp auth information (if necessary, else leave blank)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    21
               'auth_user': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    22
               'auth_pass': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    23
               # recipient information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    24
               'rcpt_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    25
               'rcpt_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    26
               # smtp server information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    27
               'smtp_host': 'localhost',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    28
               'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    29
               'smtp_tls':  False, # True or False (use starttls on port 25/587)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    30
               # subject of the email
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    31
               'subject': 'Exception Notification'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    32
              }
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    33
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    34
def log_mail_error(error, timestamp):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    35
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    36
        prefix = strftime('%b %d %H:%M:%S', localtime(timestamp))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    37
        line = error.message if len(error.message) else error.args
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    38
        logfile = open(mail_error_file, 'a')
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    39
        logfile.write('%s %s: %s\n' % (prefix, error.__class__.__name__, line))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    40
        logfile.close()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    41
    except:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    42
        pass
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    43
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    44
def send_header():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    45
    write = sys.stdout.write
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    46
    #write('Content-Type: text/html; charset=utf-8\r\n')
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    47
    write('Content-Type: application/xhtml+xml; charset=utf-8\r\n')
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    48
    write('Status: 500 Internal Server Error\r\n\r\n')
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    49
    sys.stdout.flush()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    50
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
def send_error_page():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    52
    send_header()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    53
    print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    54
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    55
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    56
  <head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    57
    <title>Internal Server Error</title>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    58
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    59
  </head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    60
  <body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    61
    <h1>Internal Server Error</h1>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    62
    <p>So wie es aussieht, ging bei der Bearbeitung Ihrer Anfrage etwas
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    63
    schief.</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    64
    <p>Sofern Sie den Fehler verursacht haben, dürfen Sie jetzt ein schlechtes
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    65
    Gewissen haben.<br />
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    66
    Die Administration wird sich bei nächster Gelegenheit um das Problem
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    67
    kümmern. Bitte haben Sie dafür etwas Verständnis.</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    68
    <h2>Error 500</h2>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    69
    <p>Sorry</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    70
  </body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    71
</html>"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    72
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    73
def send_mail(info):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    74
    global mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    75
    mail_config['now'] = time()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
    mail_config['host'] = info['host'] if info['host'] != 'n/a' else 'localhost'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    77
    mail_config['date'] = strftime('%a, %d %b %Y %H:%M:%S +0000',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
            gmtime(mail_config['now']))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    79
    body = """Hi,
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    80
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    81
an uncaught exception has occurred. The details are as follows:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    83
  Type:    %(extype)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    84
  Message: %(message)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
Traceback (most recent call last):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
%(traceback)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    88
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
Additional information:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    90
  Date:     %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    91
  Request:  %(method)s %(uri)s %(protocol)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    92
  Referrer: %(referer)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    93
  Client:   %(addr)s/%(port)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    94
  U-Agent:  %(agent)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
""" % info
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
    header = """Date: %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    97
From: "%(from_name)s" <%(from_addr)s>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    98
To: "%(rcpt_name)s" <%(rcpt_addr)s>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    99
Subject: %(subject)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   100
Message-ID: <%(now)f.%(now)X@mailtb.%(host)s>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   101
Auto-Submitted: auto-generated
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   102
MIME-Version: 1.0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   103
Content-Type: text/plain; charset=utf-8
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   104
Content-Transfer-Encoding: 8bit
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   105
""" % mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   107
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
        if mail_config['smtp_port'] != 465:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
            smtp = smtplib.SMTP(mail_config['smtp_host'],
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   110
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   111
            if mail_config['smtp_tls']:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
                smtp.starttls()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   113
        else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
            smtp = smtplib.SMTP_SSL(mail_config['smtp_host'],
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   116
        if len(mail_config['auth_user']) and len(mail_config['auth_pass']):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   117
            smtp.login(mail_config['auth_user'], mail_config['auth_pass'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
        smtp.sendmail(mail_config['from_addr'], mail_config['rcpt_addr'],
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
                '\n'.join((header, body)))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   120
    except Exception, e:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   121
        # try to log it (fire and forget)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   122
        log_mail_error(e, mail_config['now'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   123
    finally:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   124
        smtp.quit()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   125
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   126
def mailtbhook(extype, exval, extb):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   127
    info = {}
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   128
    if type(extype) is type:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   129
        info['extype'] = extype.__name__
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   130
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   131
        info['extype'] = str(extype).split("'")[1]
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   132
    if hasattr(exval, 'message'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   133
        info['message'] = exval.message if len(exval.message) else 'n/a'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   134
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   135
        info['message'] = str(exval)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
    info['traceback'] = ''.join(['%s\n' % l for l in traceback.format_tb(extb)])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   137
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   138
    info['date'] = ctime()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   139
    for k in ('HTTP_HOST', 'HTTP_REFERER', 'HTTP_USER_AGENT', 'REMOTE_ADDR',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   140
             'REMOTE_PORT', 'REQUEST_METHOD', 'REQUEST_URI', 'SERVER_PROTOCOL'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
        info[k.split('_')[-1].lower()] = env[k] if env.has_key(k) else 'n/a'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
    send_mail(info)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   144
    send_error_page()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   145
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   146
def enable():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
    sys.excepthook = mailtbhook