Chapter 12 – Creating A Perl Form Part 2

In this article we will be improving our mailing form we made last week by adding a few new features. Our form worked in that last tutorial but there wasn’t any field checking to see if items were filled in.

One of the other things we will be doing is printing out header and footer files to keep pages in our template.

The code (with line numbers):

001) #!/usr/bin/perl
002)
003) use warnings;
004) use strict;
005)
006) use CGI qw/:standard/;
007) use CGI::Carp ‘fatalsToBrowser’;
008)
009) ########
010) my $admin_mail = “sulfericacid\@qwest.net”;
011) my $subject = “Someone used your form!”;
012) my $sendmail = “/usr/lib/sendmail”;
013) my $header = “header.txt”;
014) my $footer = “footer.txt”;
015) ########
016)
017) print header, start_html(“contact form”);
018) my $submit = param(‘submit’);
019)
020) open(HEADER, “< $header”) or die “Cannot open file header.txt for reading: $!”;
021) while(<HEADER>)
021) {
022) print “$_\n”;
023) }
024) close(HEADER);
025)
026) if ($submit ne “”)
027) {
028)
029) #######
030) # setup dynamic variables
031) #######
032) my $name = param(‘name’);
033) my $email = param(‘email’);
034) my $message = param(‘message’);
035) my $time = localtime();
036) #######
037)
038) if ($name eq “”)
039) {
040) print “<font color=red><center>You must fill in your name.</center></font>”;
041) print “<center>Please click your back button to continue”;
042) exit;
043) }
044)
045) if ($email !~ m/@/)
046) {
047) print “<font color=red><center>You must fill in your email address.</center></font>”;
048) print “<center>Please click your back button to continue”;
049) exit;
050) }
051)
052) if ($message eq “”)
053) {
054) print “<font color=red><center>You must fill in your message.</center></font>”;
055) print “<center>Please click your back button to continue”;
056) exit;
057) }
058)
059) open (MAIL, “| $sendmail -t”) or die “Error opening sendmail: $!”;
060) print MAIL “To: $admin_mail\n”; # required
061) print MAIL “From: $email\n”; # required
062) print MAIL “Subject: $subject\n\n”; # required
063)
064) print MAIL “Name: $name\n”;
065) print MAIL “E-mail: $email\n”;
066) print MAIL “Message: $message\n”;
067) print MAIL “\n\nThis message was sent by $ENV{‘REMOTE_ADDR’} on $time”;
068) close(MAIL) or die “Error closing sendmail: $!”;
069)
070) print “<center><b>Your message has been sent! Thank you.</b>”;
071)
072) }
073)
074)
075)
076) print <<”FORM”;
077) <form method=”POST” action=”">
078) <table width=”307″ border=”0″ cellspacing=”0″>
079) <tr>
080) <td width=”69″><div align=”right”>Name:</div></td>
081) <td width=”234″><input type=”text” name=”name” size=”50″></td>
082) </tr>
083) <tr>
084) <td><div align=”right”>Email:</div></td>
085) <td><input type=”text” name=”email” size=”50″></td>
086) </tr>
087) <tr>
088) <td><div align=”right”>Message:</div></td>
089) <td><textarea name=”message” cols=”43″></textarea></td>
090) </tr>
091) <tr>
092) <td colspan=”2″><div align=”center”>
093) <input type=”submit” name=”submit” value=”submit”>
094) </div></td>
095) </tr>
097) </table>
098) </form>
099)
100) FORM
101)
102) open(FOOTER, “< $footer”) or die “Cannot open file footer.txt for reading: $!”;
103) while(<HEADER>)
104) {
105) print “$_\n”;
106) }
107) close(FOOTER);
108)

Line by line Explanation (new colored lines)

lines (13-14)
Setting up two variables for two text files. These two files will be used to create a template for
our script so it fits in our site layout. Since our text files are within the same folder as our form
script, we just use file.txt. If they were in a different folder, you’d have to put the file path in
there. An example would be: /home/username/public_html/header.txt.
line (20)
Using the first of the two new variables we created. We are opening $header (header.txt) for
reading. We are creating the filehandle HEADER as we don’t edit the file directly, we edit the
file in memory and it transfers when we close the file handle.
lines (21-23)
Our filehandle HEADER is open, to print the contents line-by-line into our script (to make the
layout around the mailing form) we place our print in a while loop. A while loop will continue
until there is nothing left, in this case it’ll print every line until there’s no line left for it to find.
line (24)
It saves us headaches down the road if we close our filehandles the minute we’re done with
them. The longer you keep them open the better chance you have of accidently editing the
contents of it. We printed all that we had in HEADER, so we’re closing it.
line (35)
We are making a new variable $time with the system built-in function localtime(). This tells us
when the script was executed.
lines (38-43)
The first version of our script sent out an email no matter if the user filled in all three text fields
or if they filled in zero of them. We are checking $name against “” (which is an empty
comparison) to see if it was filled in.

If the $name field was blank, we print a few messages telling them what they forgot then we
exit;. Exit slaps Perl in the face and makes it quit the script regardless of what it was doing at
the time.
lines (45-50)
The email field is one of the most difficult things to check against, there actually isn’t a sure
way to do this without sending an email to the address they posted requiring authentication
the account is alive.

This is the most simplistic way (definitely not near one of the better choices, but it’s a learning
tool) is to test $email to see if it contains a @ sign. All email addresses have one in it, so if
what they submitted doesn’t have one, we know they’re lying or very forgetful.

If this doesn’t suit you, pull out the regexes ladies and gentlemen and create a nice hack.
lines (52-57)
Exactly the same as lines 45-50, we are testing to see if they included something in the
message. If not, we exit the program after complaining to them.
line (67)
There are a few built in variables we can include in our mailing script. Two of the more useful
ones are the user’s IP address and the time they sent the email. The IP address of the script
user is stored in $ENV{‘REMOTE_ADDR’}.

If you want to see the full list of %ENV variables, use a foreach loop and print them out. You
can find out a lot of information from your users this way.

$time is calculated by localtime() we defined earlier in the script..
lines (102-107)
Identical to what we did with the header.txt file earlier. We are printing out our footer.txt
($footer) which contains the rest of our HTML code for our layout.
Complete script (without lines) for copying and pasting

#!/usr/bin/perl

use warnings;
use strict;

use CGI qw/:standard/;
use CGI::Carp ‘fatalsToBrowser’;

########
my $admin_mail = “your\@email.com”;
my $subject = “Someone used your form!”;
my $sendmail = “/usr/lib/sendmail”;
my $header = “header.txt”;
my $footer = “footer.txt”;
########

print header, start_html(“contact form”);
my $submit = param(‘submit’);

open(HEADER, “< $header”) or die “Cannot open file header.txt for reading: $!”;
while(<HEADER>)
{
print “$_\n”;
}
close(HEADER);
if ($submit ne “”)
{
#######
# setup dynamic variables
#######
my $name = param(‘name’);
my $email = param(‘email’);
my $message = param(‘message’);
my $time = localtime();
#######

if ($name eq “”)
{
print “<font color=red><center>You must fill in your name.</center></font>”;
print “<center>Please click your back button to continue”;
exit;
}

if ($email !~ m/@/)
{
print “<font color=red><center>You must fill in your email address.</center></font>”;
print “<center>Please click your back button to continue”;
exit;
}

if ($message eq “”)
{
print “<font color=red><center>You must fill in your message.</center></font>”;
print “<center>Please click your back button to continue”;
exit;
}

open (MAIL, “| $sendmail -t”) or die “Error opening sendmail: $!”;
print MAIL “To: $admin_mail\n”; # required
print MAIL “From: $email\n”; # required
print MAIL “Subject: $subject\n\n”; # required

print MAIL “Name: $name\n”;
print MAIL “E-mail: $email\n”;
print MAIL “Message: $message\n”;
print MAIL “\n\nThis message was sent by $ENV{‘REMOTE_ADDR’} on $time”;
close(MAIL) or die “Error closing sendmail: $!”;

print “<center><b>Your message has been sent! Thank you.</b>”;

}
print <<”FORM”;
<form method=”POST” action=”">
<table width=”307″ border=”0″ cellspacing=”0″>
<tr>
<td width=”69″><div align=”right”>Name:</div></td>
<td width=”234″><input type=”text” name=”name” size=”50″></td>
</tr>
<tr>
<td><div align=”right”>Email:</div></td>
<td><input type=”text” name=”email” size=”50″></td>
</tr>
<tr>
<td><div align=”right”>Message:</div></td>
<td><textarea name=”message” cols=”43″></textarea></td>
</tr>
<tr>
<td colspan=”2″><div align=”center”>
<input type=”submit” name=”submit” value=”submit”>
</div></td>
</tr>
</table>
</form>

FORM
open(FOOTER, “< $footer”) or die “Cannot open file footer.txt for reading: $!”;
while(<HEADER>)
{
print “$_\n”;
}
close(FOOTER);

VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)