
Now that we have sent our thank you's, we have to start writing e-mails and working with log files.
What we want to do is write all of the information given by the user to a file so we can maintain a record of people that filled out your form.
With perl, you must do things in logical order
for things to work. So, the first thing we must do if we want to
work with a file is... open it!
So lets do that:
Lets dissect this line and look inside. First, the open command opens the file. It is necessary that you assign a file that Perl is going to use what is called a filehandle, that is usually typed in all upper case. I simply called it FILE. After that, we tell what file. > tells Perl to append a file. So we add the $logfile variable that we wrote earlier. Next, adding the :
|| die "I can't open $logfile\n";
kills the process if perl can't find your file. die is a simple method of killing the process if Perl can't find your file. When ever you are going to open a file or a process on your machine. Always include this, so nothing weird happens if you entered anything incorrectly!
Since the file is open, we want to write our information. And
again, we do that by using the print command. But... with one
exception this time, we want to write it to a file and not the
screen. So we tell it to print to our filehandle:
Now, we close our file:
So that fully demonstrates the associative array: in.
Almost done so bear with me. What we want to do now is, send you (the programmer) e-mail.
Now we want to take the users info and send it to another program. This process is called "piping" This line shows how it works:
There, we opened our "file" and assigned it a filehandle. But instead of treating it as a regular file, we opened a "pipe" to our sendmail program. Also we added the die statement just incase!
So we handle it just like a regular file
print MAIL "To: Your
Name <youremail@yourhost.com>\n";
print MAIL "From: $in{'usrname'}
<$in{'email'}>\n";
print MAIL "Subject: My Log report\n";
print MAIL "Someone filled out your form\n";
print MAIL "It was filled out by: $in{'usrname'}\n";
print MAIL "Here is what they said:\n";
print MAIL "$in{'comments'}\n";
close(MAIL)
There, you'll notice the To: From: and Subject: lines. This is so sendmail can properly format the message. You'll also notice the absence of the semi-colon after the:
close(MAIL)
that's because it is our last line in our program. So we can leave them off.
So, that's it. Here's what the finished product should looks like:
#!/usr/bin/perl
#MyLog CGI program to process form input from mylog.html
# Location of log file
$logfile =
"/usr/home/upstate/public_html/mylog/mylog.log";
# Location of e-mail program
$mailprog = "/usr/sbin/sendmail";
# Location of our cgi-lib.pl
file
$library = "/usr/home/upstate/public_html/cgi-bin";
# Tell Perl we require
cgi-lib.pl
require "$library/cgi-lib.pl";
# Call cgi-lib's function to
decode/parse our form input
&ReadParse;
# Print the http header
print "Content-type: text/html\n\n";
# Send the user a thank you
print "<html><head><title>Thank
you!</title</head>\n";
print "<body>\n";
print "Thanks $in{'usrname'} for filling out my
form!<br>\n";
print "I'll keep in touch!\n";
print "</body></html>\n";
# Open the log file and write
the data
open(FILE, ">$logfile") || die "I can't open
$logfile\n";
print FILE "Someone filled out your form\n";
print FILE "Here is there information:\n";
print FILE "Name: $in{'usrname'}\n";
print FILE "E-mail: $in{'email'}\n";
print FILE "Comments: $in{'comments'}\n";
print FILE "\n";
close(FILE);
# Open the sendmail program
and pipe the data
open(MAIL "| $mailprog -t") || die "I can't open
$mailprog\n";
print MAIL "To: Your Name
<youremail@yourhost.com>\n";
print MAIL "From: $in{'usrname'}
<$in{'email'}>\n";
print MAIL "Subject: My Log report\n";
print MAIL "Someone filled out your form\n";
print MAIL "It was filled out by: $in{'usrname'}\n";
print MAIL "Here is what they said:\n";
print MAIL "$in{'comments'}\n";
close(MAIL)
YOU JUST PROGRAMMED A CGI SCRIPT
Save your file as mylog.cgi and upload it. What you should do is Chmod it to 755. Create the directory: mylog and chmod that to 777. This gives permission to write to the directory. Make sure to upload the mylog.html. Now try it out!