A Form-to-mail CGI Script
Here is a simple Perl script that will perform the function of taking the data input by the user through a form and mail them. The script is a very general one, but you must modify it to adapt the text of the message to the number of fields and their names. A subroutine is used for the sake of exemplification.
#!/usr/bin/perl
# You must modify these e-mail addresses
$fromad = "web\@smith.com";
$sendto = "joe\@smith.com";
# initialize array
%contents = {};
# call subroutine
&GetData;
# get current date
chop($date = `date`);
# Now create an email message and mail it.
$subject = "Form data";
# open a named UNIX pipe to send the mail
open (MAIL, "| /usr/lib/sendmail $sendto")
|| die "Can't send mail: $!\n";
# This selects the open pipe handle.
select(MAIL);
# You must change here the field names
print <<"EMAIL";
Date: $date
From: $fromad;
To: $sendto
Subject: $subject
Name = $contents{'name'}
Email = $contents{'email'}
Phone = $contents{'phone'}
City = $contents{'city'}
State = $contents{'state'}
EMAIL
close(MAIL);
exit;
#==================================================
sub GetData {
# How many bytes are we supposed to receive?
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$contents{$name} = $value;
}
}
|
Previous | Contents | Next
|