Notice! This document is currently in Archived status.
The content of this document may be incorrect or outdated.

Print this article Edit this article

Creating a Form Using the ECN email-form

Introduction

Historically ECN has offered a email-form tool which allowed users to submit forms to a CGI script and have the information submitted emailed to an email address. Beginning in August 2003 this script was disabled because of abuse.

If you are using the Engineering Web Cluster you can use the Zope version of the

 

ECN Form Email Tool. Otherwise you will need to install a custom script to handle your form submission.

Creating the Form

The form should contain hidden elements for the following fields:

  • subject - The subject of the email
  • ack_url - The URL where the user is directed after submitting the form

Below is an example HTML form:

<form method="post" action="email.cgi">
<input type="hidden" name="subject" value="Example Email Submission">
<input type="hidden" name="ack_url" value="http://eng.purdue.edu/ECN/">

Name: <input type="text" name="Name" size="35"> <br>
Message: <textarea name="Message" rows="7" cols="45"></textarea><br>
<input type="submit" value="Send Email">
</form>

Once you have created the HTML form you will need to create the script to process the form. ECN has created a sample Python script which you can customize to handle form submissions. First Download the script (Right click and Save As). You will need to change the toaddr (To Address) and fromaddr (From Address) variables. This script can be placed in the same folder as your HTML form. The permissions need to be set such that it can be executed by everyone, to do this type:

> chmod a+rx email.cgi

In the folder where the email.cgi script was saved. Once this is complete your form should work correctly. A code listing of the script is provided below:

#!/usr/local/bin/python

import smtplib, cgi, string

form = cgi.FieldStorage()

# Change the lines below to specify the TO and
# FROM addresses

toaddr = 'login@purdue.edu'
fromaddr = 'webmaster@ecn.purdue.edu'

# Special form fields used by the email.cgi
# script

ack_url = form.getvalue('ack_url',None)
ack_text = form.getvalue('ack_text','Your submission was successful')
subject = form.getvalue('subject', '')

# form fields to skip
to_skip = ['ack_url', 'ack_text', 'subject', 'to']

# create the email headers

msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (fromaddr, toaddr, subject)

for key in form.keys():
if string.lower(key) in to_skip: continue
msg = msg + "%s: %s\n\n" % (key, form.getvalue(key))

server = smtplib.SMTP('smtp.purdue.edu')
server.set_debuglevel(0)
server.sendmail(fromaddr, toaddr, msg)
server.quit()

if ack_url:
print "Location: %s" % (ack_url)
print

else:
print "Content-type: text/html"
print
print ack_text

Download this script (Right click and Save As)

Last Modified: Aug 1, 2023 4:07 pm GMT-4
Created: Mar 21, 2007 12:03 pm GMT-4 by admin
JumpURL: