mailtb.py
changeset 2 731c43b63c9d
parent 1 bab77be235a8
child 3 7062c52d3999
equal deleted inserted replaced
1:bab77be235a8 2:731c43b63c9d
    16 import smtplib
    16 import smtplib
    17 import traceback
    17 import traceback
    18 from os import environ as env
    18 from os import environ as env
    19 from time import ctime, gmtime, localtime, strftime, time
    19 from time import ctime, gmtime, localtime, strftime, time
    20 
    20 
    21 # make sure that the web server can write to this file
       
    22 mail_error_file = '/tmp/mailtb_error.log'
       
    23 mail_config = {# sender information
    21 mail_config = {# sender information
    24                'from_addr': '',
    22                'from_addr': '',
    25                'from_name': '',
    23                'from_name': '',
    26                # smtp auth information (if necessary, else leave blank)
    24                # smtp auth information (if necessary, else leave blank)
    27                'auth_user': '',
    25                'auth_user': '',
    32                # smtp server information
    30                # smtp server information
    33                'smtp_host': 'localhost',
    31                'smtp_host': 'localhost',
    34                'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete)
    32                'smtp_port': 25, # 25 smtp, 587 submission, 465 ssmtp (obsolete)
    35                'smtp_tls':  False, # True or False (use starttls on port 25/587)
    33                'smtp_tls':  False, # True or False (use starttls on port 25/587)
    36                # subject of the email
    34                # subject of the email
    37                'subject': 'Exception Notification'
    35                'subject': 'Exception Notification',
       
    36                # make sure that the web server can write to this file
       
    37                'error_log': '/tmp/mailtb_error.log'
    38               }
    38               }
       
    39 
       
    40 http_status = {200: 'OK',
       
    41                500: 'Internal Server Error'}
    39 
    42 
    40 def log_mail_error(error, timestamp):
    43 def log_mail_error(error, timestamp):
    41     try:
    44     try:
    42         prefix = strftime('%b %d %H:%M:%S', localtime(timestamp))
    45         prefix = strftime('%b %d %H:%M:%S', localtime(timestamp))
    43         line = error.message if len(error.message) else error.args
    46         line = error.message if len(error.message) else error.args
    44         logfile = open(mail_error_file, 'a')
    47         logfile = open(mail_config['error_log'], 'a')
    45         logfile.write('%s %s: %s\n' % (prefix, error.__class__.__name__, line))
    48         logfile.write('%s %s: %s\n' % (prefix, error.__class__.__name__, line))
    46         logfile.close()
    49         logfile.close()
    47     except:
    50     except:
    48         pass
    51         pass
    49 
    52 
    50 def send_header():
    53 def send_header(status=500, xhtml=False):
       
    54     s = status if status in http_status else 500
    51     write = sys.stdout.write
    55     write = sys.stdout.write
    52     #write('Content-Type: text/html; charset=utf-8\r\n')
    56     if not xhtml:
    53     write('Content-Type: application/xhtml+xml; charset=utf-8\r\n')
    57         write('Content-Type: text/html; charset=utf-8\r\n')
    54     write('Status: 500 Internal Server Error\r\n\r\n')
    58     else:
       
    59         write('Content-Type: application/xhtml+xml; charset=utf-8\r\n')
       
    60     write('Status: %d %s\r\n\r\n' % (s, http_status[s]))
    55     sys.stdout.flush()
    61     sys.stdout.flush()
    56 
    62 
    57 def send_error_page():
    63 def send_error_page():
    58     send_header()
    64     send_header(500, True)
    59     print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    65     print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    60   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    66   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    61 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
    67 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
    62   <head>
    68   <head>
    63     <title>Internal Server Error</title>
    69     <title>Internal Server Error</title>