Lesson 8 -- Printer Friendly Version

INSTRUCTIONS:

IMPORTANT: This version of your lesson is for saving or printing only. All links and images have been disabled to decrease download time and help you avoid printer difficulties.


Chapter 1

Today, you're going to learn how to build a CGI program that uses form data to automatically generate and send an e-mail message.

Before you start, you'll need to get in touch with your hosting service.

Once you contact your host, you'll need to get the following information:

1. The path to Perl on your server
You should already have this information, as you entered it on the first line of the programs you uploaded in lesson 5, 6, and 7. The path statement helps your program find the Perl interpreter on your server.

[usually /usr/bin/perl or /usr/local/bin/perl]

If you use Hypermart, the path to Perl is:

/usr/local/bin/perl

If you use WebProvider.com, the path to Perl is:

/usr/local/bin/perl

Please remember that this path is case-sensitive. In other words, don't use uppercase letters if your host requires lowercase letters.

2. The path to the CGI directory on your server
This is the place on the server where your CGI programs will be stored. You first obtained this information in chapter 1 of lesson 7.

If you use Hypermart.net, the server path might look something like this:

/data9/hypermart.net/username/cgi-bin/

If you use WebProvider.com, the server path to your CGI directory will look something like this:

/disk1/u/username/public_html/cgi/

(You'll want to substitute the first letter of your username for 'u,' your actual WebProvider.com username for 'username,' and your actual CGI directory for 'cgi' in the example above.

Please remember that this directory name is case-sensitive.

3. The URL of your CGI directory
This is the web address your site visitors will use to access the scripts you'll be creating. You should have obtained this information in lesson 5, 6, and 7. If you use Hypermart, your URL will look something like this:

http://username.hypermart.net/cgi-bin/

If you use WebProvider.com, your URL will look something like this:

http://username.webprovider.com/cgi/

Be sure to substitute your actual Hypermart or WebProvider user name for username and your actual WebProvider.com CGI directory name for 'cgi' in the example above. Also, please remember that your URL is case-sensitive.

4. The location and name of your sendmail program
Although you can generate an e-mail message with your CGI program, you won't be able to send that message across the net without the help of a sendmail program. A sendmail program is an application on your server designed specifically to forward your messages across the Internet.

[usually /usr/bin/sendmail or /usr/sbin/sendmail or /usr/lib/sendmail]

If you use Hypermart.net, the path to your sendmail program is

/var/qmail/bin/qmail-inject

If you use WebProvider.com, the path to your sendmail program is

/usr/lib/sendmail

Important: Please remember that this path is case-sensitive.

Note: Windows NT servers usually do not have a sendmail program for you to work with. If you don't have an account with Hypermart.net or WebProvider.com or any other hosting company with UNIX servers, you may want to skip this lesson. Fortunately, there aren't too many hosting services that rely on Windows as their server operating system, so you shouldn't have any difficulties. If in doubt, call your hosting service and ask them for the path to their sendmail program (or its equivalent).

Ready to go? Please allow me to describe the program we're about to create.

Suppose you have just created a web site for a company that has a print catalog. The company would like to mail that catalog to any and all interested parties. In order to make it as easy as possible for potential customers to order this catalog, the company asks you to create an order form on their website.

Visitors to the company's web site should be able to enter their name, e-mail address, and mailing address on this form. When the visitor finishes filling out the form, two different e-mail messages should be automatically generated.

The first message should send a thank you message to the customer. The message should be sent directly to the e-mail address the customer entered on the form.

The second message should be sent to an e-mail address leading to a company employee. The message should contain the visitor's mailing address so that the worker who receives the e-mail knows where to send the catalog.

Because there is a possibility that this employee might get reassigned or could separate from the company, the address could change and therefore should not be hard-coded into your CGI program.

Chapter 2

Let's begin by creating the HTML form our visitors will use to submit their catalog requests. This form will need to contain several text boxes:

  1. one text box will be needed to obtain the user's e-mail address;
  2. another text box will be needed to collect the user's name;
  3. we'll need another text box for the user's street address;
  4. the next text box will accept the user's city;
  5. a fifth text box will be needed for the user's state or province;
  6. the sixth box will hold the visitor's postal code;
  7. and we'll need one last text box so the visitor can tell us which country he or she is from.
We'll need to come up with a name for each text box. Let's use the following names:
  1. email
  2. custname
  3. street
  4. city
  5. state
  6. zip
  7. country
Some of our visitors might have special instructions or questions, so let's also include a textarea for additional comments. Let's name the textarea 'comments'.

And finally, we'll need to include the e-mail address of the company employee in our form. The program we'll be writing will send an e-mail to this employee each time the form is filled out, so the address will be necessary. We cannot expect our visitors to type the employee's address, so we'll need to use a hidden field to include this address on our form.

By storing the employee's address on the form instead of burying it inside of our Perl script, it will be fairly easy for the company to change this address. All they will have to do is edit the HTML form--a fairly simple procedure. Your goal as a programmer should be to deny people the opportunity to mess with your Perl scripts as much as possible. The more you do to keep unqualified users from editing your scripts, the less opportunity they will have to introduce a potentially fatal error.

Let's name the hidden employee e-mail field 'empmail'.

Notice how I used all lowercase letters for the field names. Field names are case-sensitive. By consistently using all lowercase letters, I don't have to worry about case sensitivity. All I have to do is remember to always type my field names using lowercase letters and I won't be forced to deal with introducing case-related errors when I write my program later.

Obviously, your form could have additional input devices, including checkboxes, drop-down lists, and radio buttons. However, for the purposes of this lesson, let's just go with the seven text boxes, one textarea, and one hidden field described above.

All of the input devices we'll need to create must be surrounded by <form> and </form> tags.

The initial <form> tag should point to the CGI program you'll be creating later in this lesson. I'm going to recommend that you name your program 'catalog.cgi.' If you do save your program with that name, you will want the ACTION parameter of your FORM tag to contain the URL of your CGI directory followed immediately by the name of your program file. For example, if you're a Hypermart.net user and you plan on storing your catalog.cgi program in a directory named cgi-bin, your <form> tag would look something like this:

<form action="http://username.hypermart.net/cgi-bin/catalog.cgi" 
method="post">  
If you're a WebProvider.com user and you plan on storing your catalog.cgi program in a directory named cgi, your <form> tag might look something like this:

<form action="http://username.webprovider.com/cgi/catalog.cgi" 
method="post">  
Let's roll up our sleeves and start writing the HTML code for our form. To do so, please start Notepad or a similar text editor and type the following HTML code. If the directory where you store your CGI programs is not named 'cgi-bin', please remember to use the name of your CGI directory instead of 'cgi-bin' in the <form> tag below.

Also, for the purposes of this exercise, please pretend that you are the company employee. Substitute your real e-mail address for you@yours.com in the hidden field below.

Also, don't forget that the names of your input devices (also known as field names) are case-sensitive. To avoid problems later, always use lowercase letters when typing your field names.

And finally, please be extra sure that you spell the field names correctly. Type carefully!

For more background information on HTML forms, please refer to lesson 5.

<html>
<body bgcolor="white" text="black">
<P><B>Want a catalog?</B><BR>
Please fill out our form:</P>

<form action="http://username.hypermart.net/cgi-bin/catalog.cgi" 
method="post">

<input type="hidden" name="empmail" 
value="you@yours.com">

<P>Your E-mail Address:<BR>
<input type="text" size="40" name="email">
</P>

<P>Your Name:<BR>
<input type="text" size="40" name="custname">
</P>

<P>Your Street Address:<BR>
<input type="text" size="40" name="street">
</P>

<P>Your City:<BR>
<input type="text" size="40" name="city">
</P>

<P>Your State or Province:<BR>
<input type="text" size="20" name="state">
</P>

<P>Your Postal Code:<BR>
<input type="text" size="20" name="zip">
</P>

<P>Your Country:<BR>
<input type="text" size="20" name="country">
</P>

<P>Comments or special instructions:<BR>
<textarea name="comments" rows="4" cols="40" wrap>
</textarea>
</P>

<P><input type="submit" value="Submit Request">
</P>
</form>
</body>   
</html>
When you finish typing your form, click the 'File' menu and choose 'Save As' to save your form. Choose a drive and directory and name the form 'catalog.htm' or, if your operating system supports four-letter extensions, call it 'catalog.html' instead.

Then, use your FTP program to upload this HTML file to your hosting service. Do not place this file in the same directory as your CGI scripts. Upload it to the same directory as your home page and the rest of your HTML files. Because this file is not a Perl script, it will not be necessary for you to use chmod to set permissions for this particular file.

Chapter 3

As you learned in the last lesson, each time someone fills out your form the form data is going to be posted to your CGI program in a message file named stdin.

Before your program can generate and send an e-mail message, it will need to collect the incoming form data and store it in an associative array. In this associative array, the names of your text boxes and other input devices will need to be associated with the values your visitor has typed into the input devices you placed on your form.

Once again, we will want to take advantage of the subroutine named 'ReadParse' in the cgi-lib.pl library to handle this task. Remember, this subroutine will store the incoming form data in an associative array named %in for us.

Before you proceed, please make sure that you have completed all of the activities in lesson 7. Be especially careful to verify that you have uploaded the cgi-lib.pl file to your CGI directory.

Chapter 4

Let us now create the program that will intercept the catalog.html form data and write two e-mail messages. To refresh your memory, one of these messages should go to the e-mail address supplied by our visitor, and the other should be directed to the employee address we stored in our hidden field.

WARNING: Please be very, very, very careful when typing this program. If you leave out just one semicolon or quote mark or slash or spell one word correctly, I can guarantee that your program won't run. Tracking down a missing or extraneous character can literally take hours. Work slowly and carefully, and you should have no trouble!

Please start a text editor, such as Windows Notepad or Apple's MacText or SimpleText. It is important that your CGI program be saved as a text file or it flat-out will not work.

As you learned in lesson 2, your program will need to start with the path to the perl interpreter. Although mine is /usr/local/bin/perl, yours may be different. Look at the beginning of the programs you succesfully tested in lessons 5, 6, and 7 if you don't remember your correct path.

Make sure that the path to Perl appears on the very first line of your program. The path cannot be preceded by a blank line, a space, or anything else.

#!/usr/local/bin/perl
Next, please type the two lines required to run the 'ReadParse' subroutine in the cgi-lib.pl file:

require "cgi-lib.pl"; 
&ReadParse;
If you're a WebProvider.com user, you'll probably have to include the server path to the cgi-lib.pl file in your 'require' statement, like this:

require "/disk1/u/username/public_html/cgi/cgi-lib.pl";
&ReadParse;
Where 'username' is your username, 'u' is the first letter of your username, and 'cgi' is the name of your CGI directory. If you're not sure, check with your host for the correct path to your CGI directory.

Now, let's create a variable named $mailprog to hold the location of your sendmail program. Using a variable as a stand- in for the sendmail program's location will make it easier for us to remember and utilize this information later in the program.

Another advantage of using a variable to store your sendmail program's location: if you move this program to another hosting service, you can change the path to the mail program simply by editing this line. You won't have to worry about changing the path anywhere else in your program. In most cases, your sendmail program will be saved as /usr/bin/sendmail, so your path might look like this:

$mailprog = '/usr/bin/sendmail';
If you use Hypermart, your path would look like this

$mailprog = '/var/qmail/bin/qmail-inject';
If you use WebProvider.com, your path should look like this

$mailprog = '/usr/lib/sendmail';
Please enter the path to your sendmail program only once. There should only be one sendmail program on your server.

Before we send our visitor any HTML code, we will need to set the content type. Let's take advantage of the cgi-lib.pl 'PrintHeader' subroutine we learned about in lesson 7 to set the content type. Calling the subroutine will be a lot easier than typing all that print "Content-type: text/html\n\n"; nonsense!

print &PrintHeader;  
Now, let's perform a test that will help us determine whether or not our mail program exists precisely where we said it should.

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}
The '-e' in the 'unless' test above stands for 'exists'. It is commonly used to determine whether a file is present or not. In this case, what we're saying is: unless the file identified in $mailprog exists, our program should produce a web page that displays an error message. After the web page is printed, the program should shut down (exit). Of course, if the sendmail program does exist, the lines reproduced above will be skipped entirely.

To save time, I used the 'PrintTag' trick I taught you about in lesson 3 to generate the error message. We will continue using that trick throughout this lesson and those that follow. Remember, you must not type any spaces, tabs, or other characters after the 'print <<"PrintTag";' line or in front of the initial <HTML> tag.

IMPORTANT IMPORTANT IMPORTANT Also, you mustn't type any spaces or tabs before or after the line that contains the word 'PrintTag'. The word should appear all by itself on its own line with no leading or trailing spaces. You should be able to see if there are spaces in front of the 'P', but it's a little harder to spot spaces at the end of the line. To test for trailing spaces, click your mouse in the middle of the word 'PrintTag'. Then, press the END key on your keyboard. If the cursor is leaning up against the letter 'g', you're okay. But if it's a space or two away, you're in trouble. Use your BACKSPACE or DELETE key to clean up those trailing blanks, or your program won't run.

NOTE: If you are copying and pasting my code (which I DON'T recommend) instead of typing it yourself (which I HEARTILY recommend), be aware that your browser may add a trailing space to every line and will usually insert multiple spaces in front of every line. If you didn't type the code yourself, you will need to delete the trailing and leading spaces manually with your DELETE and/or BACKSPACE keys.

Now, let's create a web page that provides our visitor with confirmation that the form data has been received and all is well.

print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
PrintTag
Again, remember to remove any spaces that precede the initial <HTML> tag or that precede or follow that final 'PrintTag' line. If you allow any spaces to creep in, the interpreter won't be able to find the tags it needs, your program will crash, and your visitors will find themselves staring at a puzzling 500 server error.

Now, let's open the mail program so we can send a message to our visitor. The line of code just below literally says: open a new message addressed to our visitor or die! If there is a problem with the mail program, our program will shut down and send an error message to our server. The error message, which must be surrounded in quotes, will be stored in a file named 'error.log' on the server. If your program crashes without explanation, you might want to check the server's error.log and see if your Can't open mail program message is stored there.

Important Note: Most mail programs require you to supply the destination e-mail address the instant you open the mail program. This is not a requirement with the mail program Hypermart uses.

Therefore, if you use Hypermart, you would want to type the following:

open (MAIL, "|$mailprog") || die "Can't open mail program\n";
But if you use any other hosting service (including WebProvider.com), you would need to include the destination e-mail address in the open statement as illustrated below. The destination e-mail address should have arrived with our incoming form data via the input device named 'email.' Therefore, the ReadParse subroutine would have stored the destination e-mail address in a variable named $in{'email'}. Those of you not using Hypermart will probably need to type the following:

open (MAIL, "|$mailprog $in{'email'}") || 
die "Can't open mail program\n";
Due to space considerations, I have had to wrap the line above over two lines. When you type the statement above, you may type it on one line.

As you learned in an earlier lesson, the vertical bars are called 'pipes.' The pipe character can be generated by shifting the backslash. Notice that there are a total of three pipe characters. Also note that there is a parenthesis in front of the word 'MAIL' and after the second quote mark.

The first pipe character tells your program to 'pipe' the upcoming message to the recipient through your mail program. The other pair of pipes are simply a stand-in for the word 'or'.

Now, let's print the first line of the e-mail message. It should contain the word "To:" followed by a space and our visitor's e- mail address, which, again was stored by the ReadParse subroutine in a variable named $in{'email'}.

The line must end with a hard return (as represented by the newline character, \n). Both the word 'To:' and the e-mail address must be enclosed in quotes. As you might suspect, the word 'To:' is case-sensitive. Be sure to type 'To:' and not 'TO:' or 'to:' and be sure to follow the colon with a space before you enter the visitor's e-mail address.

print MAIL "To: $in{'email'}\n";  
Now, let's print our reply-to and from addresses. The two addresses can be different--in other words, the message could be from one e-mail address but you could indicate here that you want replies sent to another address. For this exercise, let's extract the company e-mail address from %in and use it for both the 'Reply-To' and 'From' addresses:

print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
Again, spell 'Reply-To:' and 'From:' correctly and get the letter case right or your message may not get delivered. Also, don't forget to type a space after each colon and end each of these lines with a newline (\n) character.

Now, your message needs a subject line. You can change the subject from 'Your catalog request' to something else, if you so desire.

print MAIL "Subject: Your catalog request.\n\n";  
You must remember to always start this line with the word 'Subject,' a colon, and a space. Also, notice how the 'Subject' line is followed by two newline (\n\n) characters. Always remember to insert two blank lines between the subject and the body of your message. Your sendmail program will expect this. If you fail to type these two newline characters, your message could be damaged and may not even get delivered.

And finally, the body of our message. Feel free to edit this message to suit your needs. As you learned in lesson 2, use \n each time you want a single hard return, and \n\n if you want to double-space.

print MAIL "Dear $in{'custname'}:\n\n";   
print MAIL "Thank you for writing. This e-mail ";    
print MAIL "confirms that we have received your ";   
print MAIL "request for a catalog. Please look ";
print MAIL "for it soon!\n\n";
print MAIL "Sincerely yours,\n\n";   
print MAIL "Joe Blow\n";   
print MAIL "Big Impersonal Corporation\n";   
Notice the word 'MAIL' behind each 'print' command. The simple addition of this word causes your text to be printed to the body of the mail message. If you were to leave out the word 'MAIL', your message would wind up being displayed on a web page.

If you'd rather, you can use the 'PrintTag' trick to print the body of your e-mail message instead of typing out all those 'print' commands. Here's how:

print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that 
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag
Once again, notice the word MAIL after the 'print' command. Wasn't that much easier! If you choose to use this method, please remember not to allow any spaces or tabs to precede or follow the 'PrintTag' lines.

Now, close down the mail program and the message will be sent.

close(MAIL);  
Finally, let's mail our visitor's name and address information to the company e-mail address. We'll need to open a new mail message to the employee:

If you use Hypermart, you'd want to type:

open (MAIL, "|$mailprog") || die "Can't open mail program\n";
If you use any other hosting service, you'd need to include the recipient's e-mail address in the open statement. We want this message to go to the address stored in the hidden form field named 'empmail.' The ReadParse subroutine would have stored this value in a variable named $in{'empmail'}, so we'll need to open our mail program with the following command:

open (MAIL, "|$mailprog $in{'empmail'}") || 
die "Can't open mail program\n";
Now, let's start in on the message headers. First, the 'To:' field:

print MAIL "To: $in{'empmail'}\n";  
Let's make it look like the message is coming from our visitor:

print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
Next, let's make the subject line say 'Catalog Order':

print MAIL "Subject: Catalog Order.\n\n";  
And now for the message. Let's extract values from %in to reproduce all of the data captured from the form fields named custname, street, city, state, zip, country, and comments:

print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag
Remember to keep that final 'PrintTag' line clean. Don't let any spaces or tabs sneak in front of or behind the code on that line.

Let's not forget to close the mail program:

close(MAIL);  
That's it! Click the 'File' menu and choose 'Save As' to save your work. Place the file on your computer in the same drive and in the same folder as your cgi-lib.pl and catalog.htm files. Name this file catalog.cgi. Here's the complete code for Hypermart users (WebProvider.com users can find their version of the same program further down the page):

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
PrintTag

#open mail program
open (MAIL, "|$mailprog") || 
die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 



And here's the complete code for all you WebProvider.com users:

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in  
#remember to change u, username and cgi as needed 
require "/disk1/u/username/public_html/cgi/cgi-lib.pl";
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/usr/lib/sendmail';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
PrintTag

#open mail program
open (MAIL, "|$mailprog $in{'email'}") || 
die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog $in{'empmail'}") || 
die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 

Chapter 5

It is now time to upload the catalog.cgi file to your server.

First, make sure you have uploaded the catalog.htm or catalog.html file to your server. Because this form is nothing more than a simple web page, remember to place this file in the same directory as your home page and all your other web pages. As I mentioned before, it will not be necessary for you to set the file permissions for this file.

Secondly, Instruct your FTP program to go to the directory you have set aside for CGI programs. This directory is usually called cgi-bin or cgi. If you're using WS-FTP 95, all you have to do to enter the directory is double-click. Make sure that the cgi-lib.pl library file has already been uploaded to this directory.

Next, set your FTP program to transmit in ASCII (text) mode and send the catalog.cgi file to the special CGI directory. Make sure the file is named catalog.cgi and not Catalog.cgi or CATALOG.cgi or catalog.txt or some other variation. If the name is not correct, rename the file before you upload!

Once your file reaches the special CGI directory on your server, point your mouse at the catalog.cgi file on your server, click the left mouse button once to select the file, and then click the right mouse button. A menu should pop up. Somewhere on this menu, you will find the UNIX chmod command. Click your left mouse button on chmod and set your file permissions as follows:

Owner - read, write, and execute privileges.

Group - read and execute privileges.

Other/World - read and execute privileges only.

When you finish uploading and setting file permissions for the catalog.cgi file, you're ready to test your program!

Stay logged onto the Internet.

Next, start your web browser and type in the URL of your catalog.html file. For example, if your file is named catalog.htm and it is located at http://www.yourcompany.com, you would want to type the following:

http://www.yourcompany.com/catalog.htm

If you're a Hypermart member, you'd type something like this (being careful to substitute your real user name for username:

http://username.hypermart.net/catalog.htm

If you're a WebProvider.com member, you'd probably type (being careful to substitute your real user name for username:

http://username.webprovider.com/catalog.htm

The form you created in chapter 2 of this lesson should appear on your screen. If it does not appear, fire up your FTP program and try uploading the HTML file again. Check the name of the file carefully--make sure it is spelled correctly. Also, make sure that you are uploading this file into your HTML directory and not into your CGI directory. The two types of files are usually segregated from one another.

Once the form appears, type your e-mail address and name in the designated text boxes and click the 'Submit' button.

After a brief pause, you will see one of the following messages:

Message One


Thank You!

Thanks for filling out our form.

Check your e-mail for a message from us.


EXPLANATION: If you receive this message, your program worked perfectly. No further modification will be necessary. If you entered your e-mail address in the text box, check your mail and you should have a message waiting! Although the message should arrive within 10-20 minutes in most cases, please be aware that some hyper-busy mail providers (such as AOL and Hotmail) can delay the receipt of your mail by up to 24 hours. Be patient--if you received the confirmation message, your mail will arrive! If the mail still doesn't arrive after 24 hours, then there is a small typographical error in your script or form: you either spelled the name of your 'email' or 'empmail' fields incorrectly, or you spelled your e-mail address incorrectly on the HTML form.

Message Two


403 Forbidden

Your client does not have permission to get URL cgi-bin/catalog.cgi from this server.


EXPLANATION: If you see this message, it means that you have not set file permissions properly. The world needs to be able to read and execute your cgi file.

SOLUTION: Use your FTP program's chmod command to change the file permissions. If you use WS_FTP, you can right- click on the file after you've uploaded it to your CGI directory. Give the owner read, write and execute privileges. Give the group read and execute privileges. Give the world (also known as other) read and execute privileges.

If you have Telnet and know how to use it, you could Telnet to your server and enter the cgi-bin subdirectory. Then, issue the following UNIX command: chmod 755 catalog.cgi

Message Three

500 Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.


EXPLANATION: This is the most common error. It is always caused by a typo in your program or by forgetting to set file permissions or upload a needed file. If you see this error, then:

SOLUTION: Look for and correct spelling errors. Make sure you didn't leave out a quote mark, semicolon, parenthesis, or bracket. I find that a missing semicolon at the end of a line or an extra quote mark or an incorrect path is almost always to blame for this error. Sometimes a second set of eyes can help you spot the typo. Have a friend, coworker or relative look over your code and compare it to mine to find the error. If that doesn't solve your problem, delete the program and cgi-lib.pl files from your server, re-upload them in ASCII mode, and set the file permissions again. If that doesn't work, compare your cgi-lib.pl file to mine and make sure they are identical.

If you can't figure out where the typo is, then ask your host to let you see the entries in the error.log file on your server (Hypermart does this for you automatically every time you have an error). The error.log file will display a message that usually tells you exactly which line of your program contains the typo or which file doesn't have its permissions set correctly.

Message Four


404 Not Found

The requested URL cgi-bin/catalog.cgi was not found on this server.


EXPLANATION: The server cannot find the file specified in the <form> tag of your catalog.html file.

SOLUTION: Make sure that you have uploaded the catalog.cgi file to the directory specified in your catalog.html file's <form> tag. If the catalog.cgi file is in the correct directory, then your <form> tag contains an error. Make sure you spelled the name of the catalog.cgi file correctly, using all lower case (file names are case-sensitive). If the file name is spelled correctly and the case is right, then your <form> tag is probably not pointing to the right directory. Ask your hosting service what your <form> tag needs to say so that your form can call up the catalog.cgi program.

Message Five

Well, this really isn't a message, but sometimes your CGI file will just print out on the screen. Instead of running, the program will just display your source code.

EXPLANATION: The server isn't executing your file.

SOLUTION: The server either can't find the Perl interpreter or doesn't recognize the file extension you used. Make sure that your catalog.cgi file name ends with a .cgi extension and not .txt or some other extension. If it does, look at the first line of your CGI file. Does it have a number sign (#) followed immediately by an exclamation point (!) and the correct path to perl? Check with your hosting service. Ask them what extension you should use on your CGI programs and what the very first line of your CGI program should say.

Message Six

This one isn't a message either, but there's a slight chance that the program could generate the 'Thank You' message, but when you check your mail there is no message there.

EXPLANATION: There are four possible explanations for this:

1) the Internet is incredibly busy, your Internet Service Provider is backlogged, or your mailbox is full;

2) you entered your e-mail address incorrectly on the form; or

3) you made a typo on the line that opens your mail program or on the 'To:' line of your message header; or 4) your mail program requires the e-mail address immediately after it opens and you forgot to include the recipient's address in the open command (this is not a problem with Hypermart):

open (MAIL, "|$mailprog $in{'email'}") || 
die "Can't open mail program\n";
SOLUTION: If you suspect problem 1) is the culprit, try again an hour or two later . If you suspect 2) is correct, fill out the form again, typing your e-mail address more carefully. If you think 3) is possible, check your CGI program for typographical errors. In other words, make sure you didn't type $In{'email'} or $IN{'email'} or $in{'Email'} or $in{'e-mail'} instead of $in{'email'}. Also note that 'email' should be surrounded by {curly brackets}, not (parentheses). If you suspect that 4) is causing your problems, substitute the 'open' command above for the 'open' command you're using.

Message Seven


Cannot find mail program.

There is a typo in the mail program path.


EXPLANATION: This is the error message you yourself created. If you see this message, it means that there is a typo on the line you use to assign a value to $mailprog.

SOLUTION: Check with your hosting service to find the correct path to your mail program.

Chapter 6

There will be times when an error you've made will keep your program from running. As I mentioned before, your server's error.log file will usually tell you where you made the typo. However, there will be times when your error.log simply reports that there has been a 'premature end of script headers', and that's it. No line number. No further explanation. All the message is telling you is that you have made a mistake that is causing your script to end prematurely--a fact you undoubtedly already knew. The problem is that the message doesn't give you enough information to pinpoint your typo.

To find your error, I recommend that you first re-upload your files and set your permissions as described earlier in this lesson. If that doesn't solve your problem, print and read your code carefully, scanning for errors of the type described in the previous chapter. If you still can't find the error, try using a breakpoint to more precisely locate your error.

What's a breakpoint? Well, it's a bit of code you can use to find the point where your program is breaking. Here's how it works: First, you insert the breakpoint code into your program. Then, you upload, set permissions, and run your program to see if the breakpoint code works. If it does, you move the breakpoint code down a few more lines, upload and run your program again. Repeat the process over and over again, and you'll eventually find the part of the program that holds your mistake.

For example, take a look at the program below. It contains an error on line 37--there are three forbidden spaces in front of the word 'PrintTag'.

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR=\"white\" TEXT=\"black\">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
   PrintTag

#open mail program
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 

Let's say that you've uploaded this program and the cgi-lib.pl file to the proper directory, set the permissions correctly, and the program won't run. You give it the once-over, but your tired eyes fail to spot the error.

What to do? Well, let's try setting a breakpoint. What we'll do is insert the following lines at a key point in our program (by the way, that's a 'zero' between the parentheses):

print "<HTML><BODY><P>Made it!</P></BODY></HTML>";
exit(0);

The code reproduced above can be placed almost anywhere inside your script, as long as it follows the 'print &PrintHeader;' line. This is because you must always set the content type before you try to produce any HTML code.

Let's begin by setting our breakpoint just before the 'unless' statement:

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

#breakpoint code follows
print "<HTML><BODY><P>Made it!</P></BODY></HTML>";
exit(0);
#breakpoint code above

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR=\"white\" TEXT=\"black\">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
   PrintTag

#open mail program
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 

You would now want to upload your program and run it. The program should print the words 'Made it!' and then exit, ignoring all lines that follow. If it fails to print those words, then your error would have to lie on one of the lines above your breakpoint. Perhaps you forgot to upload the cgi-lib.pl file, or you left out a quote, or you spelled 'PrintHeader' incorrectly.

But let's say you met with success and that the words 'Made it!' did appear. That means that your error can be found a little further down the page. In order to pinpoint your error, You'll want to move the breakpoint down a few lines. Let's place it after the 'unless' statement:

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#breakpoint code follows
print "<HTML><BODY><P>Made it!</P></BODY></HTML>";
exit(0);
#breakpoint code above


#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR=\"white\" TEXT=\"black\">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
   PrintTag

#open mail program
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 

You'll now want to save, re-upload, and test your file again. Suppose that it runs as expected, printing the words 'Made it!' and exiting. If so, your mistake is yet further down the page. To detect your error, you'll have to move the breakpoint once more:

#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR=\"white\" TEXT=\"black\">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
   PrintTag

#breakpoint code follows
print "<HTML><BODY><P>Made it!</P></BODY></HTML>";
exit(0);
#breakpoint code above

#open mail program
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'empmail'}\n";   
print MAIL "From: $in{'empmail'}\n";
print MAIL "Subject: Your catalog request.\n\n";  

#print body of message
print MAIL << "PrintTag";
Dear $in{'custname'}:

Thank you for writing. This e-mail confirms that
we have received your request for a catalog. 
Please look for it soon!

Sincerely yours,

Joe Blow
Big Impersonal Corporation
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

#open mail program for a second message to employee
open (MAIL, "|$mailprog") || die "Can't open $mailprog\n";

#print message headers
print MAIL "To: $in{'empmail'}\n";  
print MAIL "Reply-To: $in{'email'}\n";   
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: Catalog Order.\n\n";  

#create body of message
print MAIL << "PrintTag";
Please rush a catalog to:

$in{'custname'}
$in{'street'}
$in{'city'} $in{'state'} $in{'zip'}
$in{'country'}

$in{'comments'}
PrintTag

#close mail program and send message
close(MAIL);  

#end of program 

After saving, uploading, and testing your program, once again, let's say that your program crashes. You never see the words 'Made It!' this time. That can only mean one thing: your error lies between this breakpoint and the last breakpoint you set. In other words, the error is within the code that produces your confirmation web page. A close inspection of that code will reveal your mistake: the extra spaces that precede 'PrintTag'. Remove those spaces (and the breakpoint), and your code will run flawlessly.

IMPORTANT: If your program generates a 500 server error and fails to run no matter where you insert your breakpoint code, then there is a serious error in your program that is preventing the interpreter from making any sense of your commands whatsoever. Before the interpreter processes any command, it gives the program a once-over, matching all open curly brackets with closing curly brackets, matching open quotes with close quotes, and making sure each line ends with a semicolon. If you're missing a quote or semicolon or curly bracket or have made another similarly grievous error, then the interpreter will shut you down before it even looks at your breakpoint code. Therefore, if you can't even get your breakpoint to work, you should check your code carefully for a very serious typographical error. Such an error should be easy to spot.

What if you still cannot find your error?

If you've tried everything suggested in this lesson and you still don't know why your program won't run, please remember that this lesson is accompanied by a discussion area. If you feel like you need a helping hand, just post a message in the lesson 8 discussion area. If you're feeling swamped, I and your fellow students will be more than happy to lend a hand. However, we won't be able to help you unless you provide us with plenty of information. If you need to post a message asking for help troubleshooting your code, please remember to include:

Chapter 7

-Quiz 8-

When you finish reading this lesson but before you upload and test your programs on a server, I have a quiz I'd like you to complete. Please click the word 'Quizzes' at the top or bottom of this page. Fill out the form and click the round option button in front of the words 'Quiz 8'. Then, click the 'Submit' button to take the quiz. Good luck!

-Assignment F-

First, upload and run the form, cgi-lib.pl file, and CGI script described in the first five chapters of this lesson. Once you get that program working, I'd like you to try your hand at another:

First, create a form with three text boxes. Ask the visitor to type their e-mail address in the first text box, their first name in the second text box, and their favorite color in the third text box. Place a hidden field on the form, as well. Use this hidden field to hold your e-mail address.

Then, create a cgi program that will do two things:

1. verify that the user entered an e-mail address in the first text box. If the text box is empty, have your program create a web page containing a message that explains the error and encourages the user to try again. Make sure your program shuts down after producing this web page.

2. automatically send the visitor an e-mail message similar to the one below:


To: visitor@suchandso.com
Reply-To: you@yourcompany.com
From: you@yourcompany.com
Subject: Assignment F

What a coincidence, Craig...

My favorite color also happens to be blue!

Small world, eh?

-Your name


I want your program to actually use the incoming form data. In other words, write the program so it will substitute the visitor's actual e-mail address for visitor@suchandso.com, the visitor's name for Craig, and the visitor's favorite color for blue. Also, make sure your program substitutes your e-mail address (which you've hidden on the form) for you@yourcompany.com.

For example, if your e-mail address really were 'you@yourcompany.com' and a visitor entered 'sally@earthlink.net' in the first text box, 'Sally' in the second text box, and 'red' in the third text box on your form, the message should read:


To: sally@earthlink.net
Reply-To: you@yourcompany.com
From: you@yourcompany.com
Subject: Assignment F

What a coincidence, Sally...

My favorite color also happens to be red!

Small world, eh?

-Your name


Name your HTML form 'color.htm'. Upload it to the same directory as the 'catalog.htm' file you created earlier. Name the CGI program 'color.cgi'. Upload it to the same directory and give it the same permissions as the catalog.cgi and cgi-lib.pl files you worked with earlier in this lesson. Test and debug your form and program until they both work flawlessly.

You should be able to tell if your program is working or not, so there is no need to send me anything. However, please post a message in the Discussion Area if you need help.

If you're a Hypermart user, a possible solution for this assignment resides below. However, you'll learn more by creating your own program from scratch. Try not to look unless you absolutely, positively have to do so!



color.htm


<html>
<body bgcolor="white" text="black">
<P><B>What's your favorite color?</B></P>

<form action="cgi-bin/color.cgi" method="post">

<input type="hidden" name="mymail" 
value="you@yours.com">

<P>Your E-mail Address:<BR>
<input type="text" size="40" name="email">
</P>

<P>Your First Name:<BR>
<input type="text" size="40" name="firstname">
</P>

<P>Your Favorite Color:<BR>
<input type="text" size="40" name="color">
</P>

<P><input type="submit" value="Submit Color">
</P>
</form>
</body>   
</html>



color.cgi


#!/usr/local/bin/perl

#run subroutine to turn stdin into an array named %in   
require "cgi-lib.pl"; 
&ReadParse;

#store location of mail program in variable   
#your mail program's locale may differ   
$mailprog = '/var/qmail/bin/qmail-inject';

#use cgi-lib.pl subroutine to set content type  
print &PrintHeader;

#verify that path to mail program is correct
unless (-e $mailprog)
{
print <<"PrintTag";
<HTML><BODY>
<H3>Cannot find $mailprog.</H3>
<P>There is a typo in the mail program path.</P>
</BODY></HTML>
PrintTag
exit(0);
}

#verify that something has been typed in 'email' field.
if ($in{'email'} eq "")
{
print <<"PrintTag";
<HTML><BODY>
<H3>Please provide an e-mail address</H3>
<P>We can't write back without one!</P>
</BODY></HTML>
PrintTag
exit(0);
}

#create confirmation web page
print <<"PrintTag";
<HTML><HEAD>
<TITLE>Thanks for responding!</TITLE>
</HEAD>
<BODY BGCOLOR="white" TEXT="black">
<H2>Thank You!</H2>
<P>Thanks for filling out our form.</P>
<P>Check your e-mail for a message from us.</P>
<!-- change the URL below to YOUR home page -->
<a href="http://you.yourco.com">Home</a>
</body></html>
PrintTag

#open mail program
open (MAIL, "|$mailprog") || 
die "Can't open mail program\n";

#print message headers (to, from, subject, etc)
print MAIL "To: $in{'email'}\n";  
print MAIL "Reply-To: $in{'mymail'}\n";   
print MAIL "From: $in{'mymail'}\n";
print MAIL "Subject: Your favorite color\n\n";  

#print body of message
print MAIL << "PrintTag";
What a coincidence, $in{'firstname'}...

My favorite color also happens to be $in{'color'}!

Small world, eh?

Mary Perez
PrintTag

#close mail program, releasing message to be sent
close(MAIL);  

IMPORTANT: This version of your lesson is for saving or printing only. All links and images have been disabled to decrease download time and help you avoid printer difficulties. Do NOT attempt to click any of the links on this page.

Copyright 1999 by ed2go.com. All rights reserved.
No reproduction or redistribution without written permission.

1