0

Chapter 12 – Creating A Perl Form Part 2

-

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)
0

Chapter 11 – Creating a Perl Form Part 1

-

Chapter 11 – Creating A PerlĀ Form Part 1

In this article we’ll be creating a very simplistic contact form. We all know what contact forms are, but now that we’ve learned a bit of Perl, it’s time to put projects together and make physical scripts you can use.

This contact form is setup and tested for accuracy, this does work on this server and with very minor alterations it will work on yours as well. This is as basic as it gets, there is minimal error checking and features because this will be a growing project for us.

We will continue to build on this form to make it more stable, visually pleasing and to add more features.

The code (with line numbers):

01) #!/usr/bin/perl
02)
03) use warnings;
04) use strict;
05) use CGI qw/:standard/;
06) use CGI::Carp ‘fatalsToBrowser’;
07)
08) ########
09) my $admin_mail = “you\@email.com”;
10) my $subject = “Someone used your form!”;
11) my $sendmail = “/usr/lib/sendmail”;
12) ########
13)
14) print header, start_html(“contact form”);
15) my $submit = param(‘submit’);
16)
17) if ($submit ne “”)
18) {
19)
20) #######
21) # setup dynamic variables
22) #######
23) my $name = param(‘name’);
24) my $email = param(‘email’);
25) my $message = param(‘message’);
26) #######
27)
28) open (MAIL, “| $sendmail -t”) or die “Error opening sendmail: $!”;
29) print MAIL “To: $admin_mail\n”; # required
30) print MAIL “From: $email\n”; # required
31) print MAIL “Subject: $subject\n\n”; # required
32)
33) print MAIL “Name: $name\n”;
34) print MAIL “Message: $message\n”;
35) close(MAIL) or die “Error closing sendmail: $!”;
36)
37) print “Your message has been sent! Care to send another?”;
38)
39) }
40)
41)
42) print <<”FORM”;
43)
44) <form method=”POST” action=”">
45) <table width=”307″ border=”0″ cellspacing=”0″>
46) <tr>
47) <td width=”69″><div align=”right”>Name:</div></td>
48) <td width=”234″><input type=”text” name=”name” size=”50″></td>
49) </tr>
50) <tr>
51) <td><div align=”right”>Email:</div></td>
52) <td><input type=”text” name=”email” size=”50″></td>
53) </tr>
54) <tr>
55) <td><div align=”right”>Message:</div></td>
56) <td><textarea name=”message” cols=”43″></textarea></td>
57) </tr>
58) <tr>
59) <td colspan=”2″><div align=”center”>
60) <input type=”submit” name=”submit” value=”submit”>
61) </div></td>
62) </tr>
63) </table>
64) </form>
65)
66) FORM
67)

Line by line Explanation:

lines (1-4)
Setting up our script and turning on warnings and strict to help us debug in the off-chance something goes terribly wrong.
lines (5-6)
To make this into a CGI script, we use the CGI module. We also turn on fatalsToBrowser
which is another debugging aid that works specifically for CGI. This line should be removed
after the design and implementation of the script, but use these often to help troubleshoot problems.
lines (9-10)
Here we are setting up the static variables for the form. We are setting our the administrator’s email address and the subject of the email we want displayed.

The email address needs to have the escape character \ before the @ sign. This is because
the @ is actually used by the array variable. And to prevent this from happening, we escape
it and treat the @ character as text.
line (9)
We are setting up another variable, this time with the location of sendmail on our server. This location can differ from server to server but this is the one that’s used the majority of the time. Sendmail is one of many Perl modules to email data.
line (14)
We are printing out the HTTP headers. Before we can print any information to our web
site, we need to print the headers. start_html(”) is the title of the page by default.
line (15)
We are making a variable with the contents from the form paramaters. my $var =
param(“FORM_FIELD_NAME”); is how we take the information from our textlines, text
areas, check boxes, etc. The name of our form field is the name of our param(“”); and we
store this into our variable so we can use it.

If you look at the HTML source for our form, you’ll see that our form submit button is
named submit so this is the value we are storing in $submit.
line (17)
This line is testing our variable for existence. Literally, it is checking to see if our variable
does not equal an empty string (or a false value). If $submit (which is the value of our form)
has a value– which means it was pressed– then the code in the braces will be executed.
line (18)
This is the beginning of our execution block for line 17. If our test on line 17 comes back
true, everything starting from this line until line 39 will be executed.
lines (23-25)
We are creating more variables from our form fields. Our HTML form has three user-defined
fields: name, email address and message. We are storing these parameters into these three
variables.
line (28)
We are opening up the sendmail module and storing it in our filehandle MAIL. We are
using taint mode -t because it helps the security a little.
lines (29-34)
Here we are printing our information to our filehandle. This is where our email message
actually comes from. To send an email you need a TO:, FROM: and SUBJECT: field. You
need to add a \n to the end of these fields (the SUBJECT field requires 2 \n\n).

The other fields are always optional, but you might want some information to send, right? :)
So we are also printing the name and message from the user. These optional fields do not
require you to use a description and a colon (Ie. Name: ), everything here on out is printed as
text and will be seen in your email. You could use print MAIL “$name”; if you wanted.
lines (35-37)
We are done printing our information to our filehandle so we close it. At this time our email
is sent (or in the process of being sent). Line 37 is just a message to let them know the
contact form did something.
line (39)
We are done with our emailing, so we close the brace we started on line 18. Everything
following this will be executed each time the script is run.
line (42)
This is a heredoc. This is how you print multiple lines of code with a single print statement,
we could use print “”; line by line but using a heredoc saves our fingers and our sanity.
This will print everything as text (with the exception of scalars which retain their values) until
it reaches the line you started.

Our heredoc starts with the word FORM, to end a heredoc you need to enclose the word
FORM on it’s own line without trailing spaces. You also need a blank line underneath it.
lines (43-64)
This is our entire HTML source code for the form. You can add an entire page of source
code here if you want to spaz it up a little bit. One thing you need to make sure to do, when
you are setting up your HTML form field names, be sure you set up the variables for them
earlier in the script.
lines (66-67)
Our heredoc is ended because it reaches FORM. Line 67 is a not option, you need a new
line under the closing line of a heredoc.

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 = “you\@email.com”;
my $subject = “Someone used your form!”;
my $sendmail = “/usr/lib/sendmail”;
########

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

if ($submit ne “”)
{

#######
# setup dynamic variables
#######
my $name = param(‘name’);
my $email = param(‘email’);
my $message = param(‘message’);
#######

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 “Message: $message\n”;
close(MAIL) or die “Error closing sendmail: $!”;

print “Your message has been sent! Care to send another?”;

}
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

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