# HG changeset patch # User Pascal Volk # Date 1258569606 0 # Node ID bc4d326e5a46cb38950d0e91a29a250bec85729e # Parent 7062c52d39995d54f114de85860d4cf2418aa8c0 mailtb: moved string(s) templates out of the functions. Toggled order of sending mail notification and error page. diff -r 7062c52d3999 -r bc4d326e5a46 mailtb.py --- a/mailtb.py Sat Nov 14 10:09:26 2009 +0000 +++ b/mailtb.py Wed Nov 18 18:40:06 2009 +0000 @@ -18,6 +18,52 @@ from os import environ as env from time import ctime, gmtime, localtime, strftime, time +error_page = """ + + + Internal Server Error + + + +

Internal Server Error

+

So wie es aussieht, ging bei der Bearbeitung Ihrer Anfrage etwas + schief.

+

Sofern Sie den Fehler verursacht haben, dürfen Sie jetzt ein schlechtes + Gewissen haben.
+ Die Administration wird sich bei nächster Gelegenheit um das Problem + kümmern. Bitte haben Sie dafür etwas Verständnis.

+

Error 500

+

Sorry

+ +""" +msg_body = """Hi, + +an uncaught exception has occurred. The details are as follows: + + Type: %(extype)s + Message: %(message)s + +Traceback (most recent call last): +%(traceback)s + +Additional information: + Date: %(date)s + Request: %(method)s %(uri)s %(protocol)s + Referrer: %(referer)s + Client: %(addr)s/%(port)s + U-Agent: %(agent)s +""" +msg_header = """Date: %(date)s +From: "%(from_name)s" <%(from_addr)s> +To: "%(rcpt_name)s" <%(rcpt_addr)s> +Subject: %(subject)s +Message-ID: <%(now)f.%(now)X@mailtb.%(host)s> +Auto-Submitted: auto-generated +MIME-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +Content-Transfer-Encoding: 8bit +""" config = {# sender information 'from_addr': '', 'from_name': '', @@ -36,7 +82,6 @@ # make sure that the web server can write to this file 'error_log': '/tmp/mailtb_error.log' } - http_status = {200: 'OK', 500: 'Internal Server Error'} @@ -61,7 +106,6 @@ config[k] = get('mailtb', k) config['smtp_port'] = cp.getint('mailtb', 'smtp_port') config['smtp_tls'] = cp.getboolean('mailtb', 'smtp_tls') - del(cp) def send_header(status=500, xhtml=False): s = status if status in http_status else 500 @@ -75,25 +119,7 @@ def send_error_page(): send_header(500, True) - print """ - - - Internal Server Error - - - -

Internal Server Error

-

So wie es aussieht, ging bei der Bearbeitung Ihrer Anfrage etwas - schief.

-

Sofern Sie den Fehler verursacht haben, dürfen Sie jetzt ein schlechtes - Gewissen haben.
- Die Administration wird sich bei nächster Gelegenheit um das Problem - kümmern. Bitte haben Sie dafür etwas Verständnis.

-

Error 500

-

Sorry

- -""" + print error_page def send_mail(info): global config @@ -101,33 +127,6 @@ config['host'] = info['host'] if info['host'] != 'n/a' else 'localhost' config['date'] = strftime('%a, %d %b %Y %H:%M:%S +0000', gmtime(config['now'])) - body = """Hi, - -an uncaught exception has occurred. The details are as follows: - - Type: %(extype)s - Message: %(message)s - -Traceback (most recent call last): -%(traceback)s - -Additional information: - Date: %(date)s - Request: %(method)s %(uri)s %(protocol)s - Referrer: %(referer)s - Client: %(addr)s/%(port)s - U-Agent: %(agent)s -""" % info - header = """Date: %(date)s -From: "%(from_name)s" <%(from_addr)s> -To: "%(rcpt_name)s" <%(rcpt_addr)s> -Subject: %(subject)s -Message-ID: <%(now)f.%(now)X@mailtb.%(host)s> -Auto-Submitted: auto-generated -MIME-Version: 1.0 -Content-Type: text/plain; charset=utf-8 -Content-Transfer-Encoding: 8bit -""" % config try: if config['smtp_port'] != 465: @@ -139,7 +138,7 @@ if len(config['auth_user']) and len(config['auth_pass']): smtp.login(config['auth_user'], config['auth_pass']) smtp.sendmail(config['from_addr'], config['rcpt_addr'], - '\n'.join((header, body))) + '\n'.join((msg_header %config, msg_body %info))) except Exception, e: # try to log it (fire and forget) log_mail_error(e, config['now']) @@ -163,8 +162,8 @@ 'REMOTE_PORT', 'REQUEST_METHOD', 'REQUEST_URI', 'SERVER_PROTOCOL'): info[k.split('_')[-1].lower()] = env[k] if env.has_key(k) else 'n/a' + send_error_page() send_mail(info) - send_error_page() def enable(filename=None): if filename is not None: