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']
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
mail_config = {# sender information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    22
               'from_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    23
               'from_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    24
               # 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
    25
               'auth_user': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    26
               'auth_pass': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    27
               # recipient information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    28
               'rcpt_addr': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    29
               'rcpt_name': '',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    30
               # smtp server information
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    31
               'smtp_host': 'localhost',
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    32
               '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
    33
               '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
    34
               # subject of the email
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    35
               'subject': 'Exception Notification',
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    36
               # make sure that the web server can write to this file
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    37
               'error_log': '/tmp/mailtb_error.log'
0
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
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    40
http_status = {200: 'OK',
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    41
               500: 'Internal Server Error'}
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    42
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    43
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
    44
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    45
        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
    46
        line = error.message if len(error.message) else error.args
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    47
        logfile = open(mail_config['error_log'], 'a')
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    48
        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
    49
        logfile.close()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    50
    except:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    51
        pass
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    52
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    53
def send_header(status=500, xhtml=False):
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    54
    s = status if status in http_status else 500
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    55
    write = sys.stdout.write
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    56
    if not xhtml:
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    57
        write('Content-Type: text/html; charset=utf-8\r\n')
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    58
    else:
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    59
        write('Content-Type: application/xhtml+xml; charset=utf-8\r\n')
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    60
    write('Status: %d %s\r\n\r\n' % (s, http_status[s]))
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    61
    sys.stdout.flush()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    62
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    63
def send_error_page():
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    64
    send_header(500, True)
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    65
    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
    66
  "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
    67
<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
    68
  <head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    69
    <title>Internal Server Error</title>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    70
    <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
    71
  </head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    72
  <body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    73
    <h1>Internal Server Error</h1>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    74
    <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
    75
    schief.</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
    <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
    77
    Gewissen haben.<br />
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
    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
    79
    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
    80
    <h2>Error 500</h2>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    81
    <p>Sorry</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
  </body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    83
</html>"""
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    84
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
def send_mail(info):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
    global mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
    mail_config['now'] = time()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    88
    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
    89
    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
    90
            gmtime(mail_config['now']))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    91
    body = """Hi,
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    92
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    93
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
    94
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
  Type:    %(extype)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
  Message: %(message)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    97
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    98
Traceback (most recent call last):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    99
%(traceback)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   100
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   101
Additional information:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   102
  Date:     %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   103
  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
   104
  Referrer: %(referer)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   105
  Client:   %(addr)s/%(port)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
  U-Agent:  %(agent)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   107
""" % info
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
    header = """Date: %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
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
   110
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
   111
Subject: %(subject)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
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
   113
Auto-Submitted: auto-generated
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
MIME-Version: 1.0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
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
   116
Content-Transfer-Encoding: 8bit
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   117
""" % mail_config
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   120
        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
   121
            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
   122
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   123
            if mail_config['smtp_tls']:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   124
                smtp.starttls()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   125
        else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   126
            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
   127
                    mail_config['smtp_port'])
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   128
        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
   129
            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
   130
        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
   131
                '\n'.join((header, body)))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   132
    except Exception, e:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   133
        # 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
   134
        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
   135
    finally:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
        smtp.quit()
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
def mailtbhook(extype, exval, extb):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   139
    info = {}
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   140
    if type(extype) is type:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   141
        info['extype'] = extype.__name__
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
        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
   144
    if hasattr(exval, 'message'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   145
        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
   146
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
        info['message'] = str(exval)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   148
    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
   149
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
    info['date'] = ctime()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
    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
   152
             '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
   153
        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
   154
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   155
    send_mail(info)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   156
    send_error_page()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   157
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   158
def enable():
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   159
    sys.excepthook = mailtbhook