Home
|
Helpdesk
|
Product Help
|
Showroom
|
Website development
|
Using Scripts
Contents
Introduction
The form-mail.pl Script
Using the Script
Introduction
As well as the counter script, we also provide an example form script. This will take information from an html form which it will then process and send the results in an email to a predetermined email address.
The script we provide is called form-mail.pl and a copy can be found in your cgi-bin directory. You need to edit the script to make it work.
An example html form and form-mail.pl script can be found at http://www.demon.co.uk/tools/formmail.
Instructions for setting up the form-mail.pl script
As mentioned above, you need to edit the form-mail.pl script to make it work. The text below shows what parts of the script you need to edit.
NOTE: These are only the instructions and not the actual script. If you have lost or damaged your copy of the form-mail.pl script then download it from here: http://www.demon.co.uk/tools/formmail.
The form-mail.pl script
There are a number of important things that you need to take note of when using this script. Here they are, as we break down the formmail.pl script. [Example only - Do not copy these instructions.]
#!/bin/perl
# ------------------------------------------------------------
# Form-mail.pl, by Reuven M. Lerner (reuven@the-tech.mit.edu).
# # Last updated: March 14, 1994
# # Form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site. It should be compatible with
# any CGI-compatible HTTP server.
# # Please read the README file that came with this distribution
# for further details.
# ------------------------------------------------------------
# ------------------------------------------------------------
# This package is Copyright 1994 by The Tech.
# Form-mail is free software; you can redistribute it
# and/or modify it under the terms of the GNU General
# Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any
# later version.
# Form-mail is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
# You should have received a copy of the GNU General
# Public License along with Form-mail; see the file
# COPYING. If not, write to the Free Software Foundation,
# 675 Mass Ave, Cambridge, MA 02139, USA.
# ------------------------------------------------------------
# Define fairly-constants
# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';
|
This is the correct path for the mail program on the web server.
# This should be set to the username or alias that runs your
# WWW server. $recipient = 'webmaster@your.site.here';
|
Change webmaster@your.site.here, to the email address you want the results of the script to be sent to.
# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";
# Print a title and initial heading
print "<Head><Title>Thank you</Title></Head>";
print "<Body><H1>Thank you</H1>";
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'})
# Split the name-value pairs
@pairs = split(/&/, $buffer); foreach $pair (@pairs)
{ ($name, $value) = split(/=/, $pair);
# Un-Webify plus signs and %-encoding
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Stop people from using subshells to execute commands
# Not a big deal when using sendmail, but very important
# when using UCB mail (aka mailx).
# $value =~ s/~!/ ~!/g;
# Uncomment for debugging purposes
# print "Setting $name to $value;
$FORM{$name} = $value;
}
# If the comments are blank, then give a "blank form" response
&blank_response unless $FORM{'comments'};
|
The above line checks to see if the user has entered any information in the comments field and if not calls the blank_response subroutine, see below. If you have other fields on your form that you do not want left blank, you can add extra lines. Just change the {'comments'} part to the name of the other fields in your form. You can set up separate subroutines for each blank field if you want to but its not essential.
# Now send mail to $recipient
open (MAIL, "|$mailprog $recipient")|| die "Can't open $mailprog!\n";
print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
|
The line above adds a reply address to the email that is sent to the 'webmaster@your.site.here'. It assumes that you have a field on your form called username that people will enter their email address into and a field called realname for their full name.
print MAIL "Subject: WWW comments (Forms submission)\n\n";
print MAIL "$FORM{'username'} ($FORM{'realname'})
sent the following\n";
print MAIL "comment about The Tech's WWW server:\n\n";
print MAIL "-------------------------------------
-----------------------\n";
print MAIL "$FORM{'comments'}";
|
The line above prints the input from the comments field of your form into the email. If you have other fields on your form just copy the above line and change the {'comments'} part to the name of the other fields in your form. Please note it is case sensitive. If the field on your form is called address but if you type $FORM{'ADDRESS'} here then it will not work.
print MAIL "\n-----------------------------
-----------------------------\n";
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);
# Make the person feel good for writing to us
print "Thank you for sending comments to <I>The Tech</I>!<P>";
|
You can edit the line above to put in your own thank you message.
print "Return to our <A HREF=\"/\">home page</A>, if you want.<P>";
|
You can edit the line above to include the url back to your form page or any other webpage e.g. <A HREF=\"http://www.yourdomain.co.uk/contact.htm\">home page</A>
# ---------------------------------
# subroutine blank_response
sub blank_response
{ print "Your comments appear to be blank, and thus were not sent";
print "to our webmasters. Please re-enter your comments, or";
print "return to our <A HREF=\"/\">home page</A>, if you want.<P>";
exit;
}
|
The section above is the subroutine which prints a Web page telling the person who submitted your form that they did not fill out the comments field. As mentioned above you can create separate sub routines to print a separate error message for each different field that is left blank. If you just want to get a basic mail form working though, the above routine will do fine. The only part you might want to edit is the url for the link back to your form page, as outlined in the instructions above.
Using the Script
To use the script all you have to do is call it from your html form like any other script:
<FORM METHOD="POST"
ACTION="http://cgi.www.yourdomain.co.uk/cgi-bin/
www.yourdomain.co.uk/form-mail.pl">
|
For more information on using cgi's, click here.
Helpdesk Tel: 0845 272 2555 for Commercial Web support
e-mail:
Autoresponder: ; Minicom: 0845 272 2445