Step by step, basic form processing cgi scripts

Placing a form on a web page that e-mail's the results to you is a good first introduction to cgi scripts, so we will look at how to set it in some detail:

Before you start:

Your web server must allow you to execute cgi scripts from a particular directory. If your not sure, ask your host. On EFN users are allowed to execute cgi from their public_html directory. Virtual Domains can have execcgi enabled on a particular directory.

Get a copy of a form to e-mail perl script. There are a number of free ones on the web, but make sure that it uses perl 5.0 or higher, and it fairly recent. Older scripts (like Matt's script archive formail before vs 1.9) can have security issues - someone could use your account to broadcast spam, not a good thing.

A good article of what to look for in a cgi script can be found at . Some good updated replacements for the common cgi scripts can be found at sourceforge - nms  A couple other possibilities - quite nice. or

1. Make a directory named cgi-bin, or cgi or htbin, etc in your public html folder.

2. Unpack your free script and open it up in a text editor (not a word processor) - something like Textpad, UltraEdit, etc and follow directions to configure the script:

Set the top line to point to the location of the perl program on your server, (on EFN it would be #/usr/bin/perl5). You will undoubtedly need to enter your domain name, and the email address to send results to and the name/location of the mail program on your server. (at EFN is usr/sbin/sendmail )

3. Save your configured script. If you are using a text editor that allows a choice between unix and dos line endings, choose unix. If you are using Windows Notepad, save it and hope. If the script you got ends in .pl or has no file extension, save it with a .cgi extension.

4. Upload your script to your server in ASCII mode. If you upload it in binary, it probably won't work (sometimes ftp programs are smart, and figure it out from the file extension)

5. On Unix systems, all files come with owners, and permissions about what can be done with the file. Next we need to set the permissions on the file so that it can be executed. We want to let users and groups read and execute, and owners read,write and execute. We do not want visitors writing to our script, as they may make changes we don't want. These permissions are often described by a number, or a set of letters. So, fire up your ftp program and find how it lets you chmod a file - and set its permissions to owner read/write/execute, group read/execute and user read/execute (755) or dwrx-rx-rx . You can also set this stuff via a shell account on the server, if you have shell access, and understand basic Unix commands. An intro to Unix commands and permissions can be found at

6. Go back to your html form and point it to your cgi script (action= cgi-bin/mymail.cgi) Upload the form, and test it.


If you get a page not found error, check the spelling, etc., and check that the permissions allow it to be read. If you get an Internal Error message, it is executing the script, but there is a problem in the script. If you have shell access, you can go to the directory where your script is, and test it with perl -cw scripname.cgi. This will check the syntax of the script. If you do not have shell access, go through steps 1-6 again, more carefully.

Other scripts - guestbooks, etc have a similar procedure, some may require an output file to write to, it needs to have read/write permissions, but not execute.

Perl form handling scripts are not difficult, but the details can be a little frustrating the first few times- good luck!

Oregon web site design and promotion guide

May 24, 2002

ML WEBB, web site design and promotion, Eugene, Oregon


© 2001 ML WEBB -
See our Privacy Policy