mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 14 Nov 2009 02:30:57 +0000
changeset 1 bab77be235a8
parent 0 3acd8a788a6f
child 2 731c43b63c9d
permissions -rw-r--r--
Added COPYING to the repository
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  -*-
1
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     3
# Copyright 2009 Pascal Volk
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     4
# See COPYING for distribution information.
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     5
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     6
__author__ = 'Pascal Volk'
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     7
__version__ = '0.1'
bab77be235a8 Added COPYING to the repository
Pascal Volk <user@localhost.localdomain.org>
parents: 0
diff changeset
     8
__date__ = '2009-11-14'
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
     9
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    10
"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    11
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
    12
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    13
"""
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
import sys
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    16
import smtplib
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    17
import traceback
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    18
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
    19
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
    20
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    21
# 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
    22
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
    23
mail_config = {# sender information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    24
               'from_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    25
               'from_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    26
               # 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
    27
               'auth_user': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    28
               'auth_pass': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    29
               # recipient information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    30
               'rcpt_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    31
               'rcpt_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    32
               # smtp server information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    33
               'smtp_host': 'localhost',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    34
               '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
    35
               '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
    36
               # subject of the email
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    37
               'subject': 'Exception Notification'
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    38
              }
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    39
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    40
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
    41
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    42
        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
    43
        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
    44
        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
    45
        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
    46
        logfile.close()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    47
    except:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    48
        pass
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    49
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    50
def send_header():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
    write = sys.stdout.write
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    52
    #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
    53
    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
    54
    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
    55
    sys.stdout.flush()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    56
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    57
def send_error_page():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    58
    send_header()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    59
    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
    60
  "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
    61
<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
    62
  <head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    63
    <title>Internal Server Error</title>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    64
    <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
    65
  </head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    66
  <body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    67
    <h1>Internal Server Error</h1>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    68
    <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
    69
    schief.</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    70
    <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
    71
    Gewissen haben.<br />
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    72
    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
    73
    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
    74
    <h2>Error 500</h2>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    75
    <p>Sorry</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
  </body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    77
</html>"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    79
def send_mail(info):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    80
    global mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    81
    mail_config['now'] = time()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
    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
    83
    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
    84
            gmtime(mail_config['now']))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
    body = """Hi,
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
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
    88
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
  Type:    %(extype)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    90
  Message: %(message)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    91
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    92
Traceback (most recent call last):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    93
%(traceback)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    94
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
Additional information:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
  Date:     %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    97
  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
    98
  Referrer: %(referer)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    99
  Client:   %(addr)s/%(port)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   100
  U-Agent:  %(agent)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   101
""" % info
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   102
    header = """Date: %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   103
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
   104
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
   105
Subject: %(subject)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
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
   107
Auto-Submitted: auto-generated
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
MIME-Version: 1.0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
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
   110
Content-Transfer-Encoding: 8bit
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   111
""" % mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   113
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
        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
   115
            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
   116
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   117
            if mail_config['smtp_tls']:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
                smtp.starttls()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
        else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   120
            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
   121
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   122
        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
   123
            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
   124
        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
   125
                '\n'.join((header, body)))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   126
    except Exception, e:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   127
        # 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
   128
        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
   129
    finally:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   130
        smtp.quit()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   131
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   132
def mailtbhook(extype, exval, extb):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   133
    info = {}
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   134
    if type(extype) is type:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   135
        info['extype'] = extype.__name__
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   137
        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
   138
    if hasattr(exval, 'message'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   139
        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
   140
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
        info['message'] = str(exval)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
    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
   143
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   144
    info['date'] = ctime()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   145
    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
   146
             '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
   147
        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
   148
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   149
    send_mail(info)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
    send_error_page()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   152
def enable():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   153
    sys.excepthook = mailtbhook