mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 14 Nov 2009 10:09:26 +0000
changeset 3 7062c52d3999
parent 2 731c43b63c9d
child 4 bc4d326e5a46
permissions -rw-r--r--
mailtb.cfg: Added to repository, for the sake of a simple module configuration. mailtb.py: Added load_config() in order to load a mailtb.cfg file. Renamed mail_config to config
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
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    21
config = {# sender information
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    22
          'from_addr': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    23
          'from_name': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    24
          # smtp auth information (if necessary, else leave blank)
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    25
          'auth_user': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    26
          'auth_pass': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    27
          # recipient information
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    28
          'rcpt_addr': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    29
          'rcpt_name': '',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    30
          # smtp server information
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    31
          'smtp_host': 'localhost',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    32
          'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete)
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    33
          'smtp_tls':  False, # True or False (use starttls on port 25/587)
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    34
          # subject of the email
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    35
          'subject': 'Exception Notification',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    36
          # make sure that the web server can write to this file
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    37
          'error_log': '/tmp/mailtb_error.log'
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    38
         }
0
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
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    47
        logfile = open(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
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    53
def load_config(filename):
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    54
    from ConfigParser import ConfigParser
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    55
    cp = ConfigParser()
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    56
    if len(cp.read(filename)):
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    57
        global config
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    58
        get = cp.get
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    59
        for k in ('auth_pass', 'auth_user', 'error_log', 'from_addr',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    60
                'from_name', 'rcpt_addr', 'rcpt_name', 'smtp_host', 'subject'):
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    61
            config[k] = get('mailtb', k)
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    62
        config['smtp_port'] = cp.getint('mailtb', 'smtp_port')
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    63
        config['smtp_tls'] = cp.getboolean('mailtb', 'smtp_tls')
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    64
    del(cp)
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    65
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    66
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
    67
    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
    68
    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
    69
    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
    70
        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
    71
    else:
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    72
        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
    73
    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
    74
    sys.stdout.flush()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    75
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    76
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
    77
    send_header(500, True)
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    78
    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
    79
  "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
    80
<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
    81
  <head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    82
    <title>Internal Server Error</title>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    83
    <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
    84
  </head>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    85
  <body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    86
    <h1>Internal Server Error</h1>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
    <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
    88
    schief.</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
    <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
    90
    Gewissen haben.<br />
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    91
    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
    92
    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
    93
    <h2>Error 500</h2>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    94
    <p>Sorry</p>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
  </body>
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
</html>"""
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
def send_mail(info):
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
    99
    global config
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   100
    config['now'] = time()
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   101
    config['host'] = info['host'] if info['host'] != 'n/a' else 'localhost'
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   102
    config['date'] = strftime('%a, %d %b %Y %H:%M:%S +0000',
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   103
            gmtime(config['now']))
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   104
    body = """Hi,
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   105
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   106
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
   107
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   108
  Type:    %(extype)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   109
  Message: %(message)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   110
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   111
Traceback (most recent call last):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   112
%(traceback)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   113
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   114
Additional information:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   115
  Date:     %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   116
  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
   117
  Referrer: %(referer)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   118
  Client:   %(addr)s/%(port)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
  U-Agent:  %(agent)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   120
""" % info
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   121
    header = """Date: %(date)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   122
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
   123
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
   124
Subject: %(subject)s
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   125
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
   126
Auto-Submitted: auto-generated
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   127
MIME-Version: 1.0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   128
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
   129
Content-Transfer-Encoding: 8bit
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   130
""" % config
0
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
    try:
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   133
        if config['smtp_port'] != 465:
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   134
            smtp = smtplib.SMTP(config['smtp_host'], config['smtp_port'])
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   135
            if config['smtp_tls']:
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
                smtp.starttls()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   137
        else:
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   138
            smtp = smtplib.SMTP_SSL(config['smtp_host'], config['smtp_port'])
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   139
        if len(config['auth_user']) and len(config['auth_pass']):
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   140
            smtp.login(config['auth_user'], config['auth_pass'])
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   141
        smtp.sendmail(config['from_addr'], config['rcpt_addr'],
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
                '\n'.join((header, body)))
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
    except Exception, e:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   144
        # try to log it (fire and forget)
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   145
        log_mail_error(e, config['now'])
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   146
    finally:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
        smtp.quit()
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
def mailtbhook(extype, exval, extb):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
    info = {}
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
    if type(extype) is type:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   152
        info['extype'] = extype.__name__
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   153
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   154
        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
   155
    if hasattr(exval, 'message'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   156
        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
   157
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   158
        info['message'] = str(exval)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   159
    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
   160
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   161
    info['date'] = ctime()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   162
    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
   163
             '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
   164
        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
   165
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   166
    send_mail(info)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   167
    send_error_page()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   168
3
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   169
def enable(filename=None):
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   170
    if filename is not None:
7062c52d3999 mailtb.cfg: Added to repository, for the sake of a simple module configuration.
Pascal Volk <user@localhost.localdomain.org>
parents: 2
diff changeset
   171
        load_config(filename)
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   172
    sys.excepthook = mailtbhook