mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Wed, 18 Nov 2009 18:40:06 +0000
changeset 4 bc4d326e5a46
parent 3 7062c52d3999
child 5 610fe1138532
permissions -rw-r--r--
mailtb: moved string(s) templates out of the functions. Toggled order of sending mail notification and error page.
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
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    21
error_page = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    22
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    23
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    24
  <head>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    25
    <title>Internal Server Error</title>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    26
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    27
  </head>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    28
  <body>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    29
    <h1>Internal Server Error</h1>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    30
    <p>So wie es aussieht, ging bei der Bearbeitung Ihrer Anfrage etwas
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    31
    schief.</p>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    32
    <p>Sofern Sie den Fehler verursacht haben, dürfen Sie jetzt ein schlechtes
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    33
    Gewissen haben.<br />
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    34
    Die Administration wird sich bei nächster Gelegenheit um das Problem
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    35
    kümmern. Bitte haben Sie dafür etwas Verständnis.</p>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    36
    <h2>Error 500</h2>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    37
    <p>Sorry</p>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    38
  </body>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    39
</html>"""
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    40
msg_body = """Hi,
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    41
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    42
an uncaught exception has occurred. The details are as follows:
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    43
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    44
  Type:    %(extype)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    45
  Message: %(message)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    46
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    47
Traceback (most recent call last):
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    48
%(traceback)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    49
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    50
Additional information:
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    51
  Date:     %(date)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    52
  Request:  %(method)s %(uri)s %(protocol)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    53
  Referrer: %(referer)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    54
  Client:   %(addr)s/%(port)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    55
  U-Agent:  %(agent)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    56
"""
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    57
msg_header = """Date: %(date)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    58
From: "%(from_name)s" <%(from_addr)s>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    59
To: "%(rcpt_name)s" <%(rcpt_addr)s>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    60
Subject: %(subject)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    61
Message-ID: <%(now)f.%(now)X@mailtb.%(host)s>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    62
Auto-Submitted: auto-generated
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    63
MIME-Version: 1.0
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    64
Content-Type: text/plain; charset=utf-8
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    65
Content-Transfer-Encoding: 8bit
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    66
"""
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
    67
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
    68
          '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
    69
          '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
    70
          # 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
    71
          '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
    72
          '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
    73
          # 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
    74
          '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
    75
          '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
    76
          # 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
    77
          '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
    78
          '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
    79
          '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
    80
          # 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
    81
          '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
    82
          # 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
    83
          '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
    84
         }
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    85
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
    86
               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
    87
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    88
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
    89
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    90
        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
    91
        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
    92
        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
    93
        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
    94
        logfile.close()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
    except:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
        pass
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    97
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
    98
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
    99
    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
   100
    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
   101
    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
   102
        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
   103
        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
   104
        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
   105
                '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
   106
            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
   107
        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
   108
        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
   109
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   110
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
   111
    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
   112
    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
   113
    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
   114
        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
   115
    else:
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   116
        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
   117
    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
   118
    sys.stdout.flush()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   119
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   120
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
   121
    send_header(500, True)
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   122
    print error_page
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   123
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   124
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
   125
    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
   126
    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
   127
    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
   128
    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
   129
            gmtime(config['now']))
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   130
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   131
    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
   132
        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
   133
            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
   134
            if config['smtp_tls']:
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   135
                smtp.starttls()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   136
        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
   137
            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
   138
        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
   139
            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
   140
        smtp.sendmail(config['from_addr'], config['rcpt_addr'],
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   141
                '\n'.join((msg_header %config, msg_body %info)))
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   142
    except Exception, e:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   143
        # 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
   144
        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
   145
    finally:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   146
        smtp.quit()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   147
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   148
def mailtbhook(extype, exval, extb):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   149
    info = {}
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   150
    if type(extype) is type:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   151
        info['extype'] = extype.__name__
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   152
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   153
        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
   154
    if hasattr(exval, 'message'):
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   155
        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
   156
    else:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   157
        info['message'] = str(exval)
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   158
    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
   159
    
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   160
    info['date'] = ctime()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   161
    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
   162
             '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
   163
        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
   164
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
   165
    send_error_page()
0
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
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
   168
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
   169
    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
   170
        load_config(filename)
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
   171
    sys.excepthook = mailtbhook