#!/usr/bin/python import cgitb; cgitb.enable() import cgi, smtplib, sys toaddr = 'form' + '@' + 'prazefarm.co.uk' header = """ Praze Farm Enquiry Form """ footer = """ """ def main(): form = cgi.FieldStorage() print "Content-type: text/html" print print header if not form: error('Empty form.') if not form.has_key('who'): error('We need your name, to know who you are.') if not form.has_key('email'): error('We need your email address in order to get in contact with you.') print "

Success

" for key in form.keys(): value = form[key].value print "

", cgi.escape(key), ":", cgi.escape(value) print '

Thank you for your interest.

' print '

Return

' print footer who = form['who'].value email = form['email'].value if form.has_key('comment'): comment = form['comment'].value else: comment = 'No comment' msg = ("From: %s\r\nTo: %s\r\n\r\n" % (email, toaddr)) msg += '\nName: %s\n\nEmail: %s\n\nComments:\n%s\n' % (who, email, comment) server = smtplib.SMTP('localhost') # server.set_debuglevel(1) server.sendmail(email, toaddr, msg) server.quit() def error(why): print "

Form incorrect

" print "

" print why print "

" print '

Please try again

' print footer sys.exit() if __name__ == "__main__": main()