1.6.6 Example

Take a look at the example below demonstrating a handler which emails information to a developer:

#!/usr/bin/env python

# show python where the web modules are
import sys; sys.path.append('../'); sys.path.append('../../../')

# set your own email here
email = 'james@example.com'

# define our custom handler
def mail(info, email, message, reply):
    import web, web.mail
    web.mail.send(
        msg=info,
        to=email,
        reply=reply,
        subject='Error in website',
        sendmail='usr/bin/sendmail',
        smtp='smtp.ntlworld.com',
        method='smtp',# could use method='sendmail' to send using sendmail.
        type='html',
    )
    print web.header()
    print message
    
# setup our handler
import web.error
web.error.handle(
    handler = mail, 
    output  = 'debug',
    email   = email,
    message = """  <html>
                   <head><title>An Error Occured</title></head>
                   <body><h1>Error Caught</h1>
                   <p>An HTML debug view of the error was sucessfully emailed to %s</p></body>
                   </html>"""%email,
    reply   = 'Developer <%s>'%email
)

# rasie a test exception and wait for the email to arrive
raise Exception('This is a test exception')

You can test this example by starting the test webserver in scripts/webserver.py and visiting http://localhost:8080/doc/src/lib/webserver-web-error-mail.py on your local machine.

Warning: If you run this example please make sure you replace the email addresses with your own email address in. You may need to change the path of sendmail or use an SMTP server instead. See the web.mail module documentation for help with this.

Note: If an exception occurs in your custom error handling function it may be difficult to track down. You can put your code inside a try except block and make sure some sensible output is returned in the event of an Exception being raised.