Mastering the Web
Contents
Website Planning Tutorial
Website Design Tutorial
HTML Tutorial
HTML Tables Tutorial
CGI Tutorial
JavaScript Tutorial
Perl Tutorials
CSS Tutorial
Installing a Web Server
Security Tutorial
HTML Cookies Tutorial
Web Tracking Tutorial
Download Free Programs
F.A.Q.

  1. Introduction to CGI
  2. CGI Environment Variables
  3. Using CGI Scripts
  4. Developing a CGI Script
  5. A Form-to-mail CGI Script

Download FWTLogstat1

Download FWTLogstat2

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

| HOME | FEEDBACK | BOOKMARK |
Build your Website
© 1999-2008 Hector Castro -- All rights reserved

If your doubt is not answered in this site, please use the
contact form .
I'll answer as soon as posible.
I can help you using instant messaging. To schedule a meeting, please use the
meeting form.
You will find the late news about the free programs offered here on my blog
Free Webmaster Tools
You can get news about updates to my free programs through this
RSS feed.

www.great-web-info.com