def verify_email(email): if not re.match(r"[^@]+@[^@]+\.[^@]+", email): return False return True f = FieldStorage() fromaddr = message = subject = None error_msg = "Looks like you have a missing field or two!" if "from" in f: fromaddr = f["from"].value if not verify_email(fromaddr): fromaddr = None error_msg = "Looks like the email you entered has a typo!" if "message" in f: message = f["message"].value if "subject" in f: subject = f["subject"].value if fromaddr and message and subject: # Success page send_email(fromaddr, subject, message) f = open('thank_you.html') print f.read() else: # Rejection page f = open('oops.html') html = f.read() html = html.format(error_msg) print html