mailtb.py
author Pascal Volk <user@localhost.localdomain.org>
Fri, 20 Nov 2009 05:08:46 +0000
changeset 5 610fe1138532
parent 4 bc4d326e5a46
permissions -rw-r--r--
mailtb.py: Reworded error_page, more serious and in English-language. Added global error_page_is_xhtml variable.
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">
5
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    23
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4
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>
5
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    30
    <p>The server encountered an internal error and was unable to complete your
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    31
    request.</p>
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    32
    <p>The administration team will take care of this problem. We apologize
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    33
    for any inconvenience.</p>
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    34
    <h2>Error 500</h2>
5
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    35
    <p>Server got itself in trouble</p>
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    36
  </body>
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    37
</html>"""
5
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
    38
error_page_is_xhtml = True
4
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    39
msg_body = """Hi,
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    40
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    41
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
    42
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    43
  Type:    %(extype)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    44
  Message: %(message)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    45
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    46
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
    47
%(traceback)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    48
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    49
Additional information:
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    50
  Date:     %(date)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    51
  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
    52
  Referrer: %(referer)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    53
  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
    54
  U-Agent:  %(agent)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    55
"""
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    56
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
    57
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
    58
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
    59
Subject: %(subject)s
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    60
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
    61
Auto-Submitted: auto-generated
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    62
MIME-Version: 1.0
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    63
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
    64
Content-Transfer-Encoding: 8bit
bc4d326e5a46 mailtb: moved string(s) templates out of the functions.
Pascal Volk <user@localhost.localdomain.org>
parents: 3
diff changeset
    65
"""
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
    66
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
    67
          '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
    68
          '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
    69
          # 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
    70
          '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
    71
          '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
    72
          # 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
    73
          '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
    74
          '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
    75
          # 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
    76
          '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
    77
          '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
    78
          '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
    79
          # 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
    80
          '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
    81
          # 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
    82
          '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
    83
         }
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
    84
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
    85
               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
    86
0
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    87
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
    88
    try:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    89
        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
    90
        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
    91
        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
    92
        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
    93
        logfile.close()
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    94
    except:
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    95
        pass
3acd8a788a6f initial commit: »don't fear the nervous delete finger«
Pascal Volk <user@localhost.localdomain.org>
parents:
diff changeset
    96
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
    97
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
    98
    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
    99
    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
   100
    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
   101
        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
   102
        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
   103
        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
   104
                '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
   105
            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
   106
        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
   107
        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
   108
2
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   109
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
   110
    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
   111
    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
   112
    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
   113
        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
   114
    else:
731c43b63c9d mailtb.py: made send_header() more flexible, added http_status dict
Pascal Volk <user@localhost.localdomain.org>
parents: 1
diff changeset
   115
        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
   116
    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
   117
    sys.stdout.flush()
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
def send_error_page():
5
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
   120
    send_header(500, error_page_is_xhtml)
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
   121
    sys.stdout.write(error_page)
610fe1138532 mailtb.py: Reworded error_page, more serious and in English-language.
Pascal Volk <user@localhost.localdomain.org>
parents: 4
diff changeset
   122
    sys.stdout.flush()
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