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
03) use warnings;
04) use strict;
05) use CGI qw/:standard/;
06) use CGI::Carp ‘fatalsToBrowser’;
08) ########
09) my $admin_mail = “you\”;
10) my $subject = “Someone used your form!”;
11) my $sendmail = “/usr/lib/sendmail”;
12) ########
14) print header, start_html(“contact form”);
15) my $submit = param(‘submit’);
17) if ($submit ne “”)
18) {
20) #######
21) # setup dynamic variables
22) #######
23) my $name = param(‘name’);
24) my $email = param(‘email’);
25) my $message = param(‘message’);
26) #######
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
33) print MAIL “Name: $name\n”;
34) print MAIL “Message: $message\n”;
35) close(MAIL) or die “Error closing sendmail: $!”;
37) print “Your message has been sent! Care to send another?”;
39) }
42) print <<”FORM”;
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>
66) FORM

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


use warnings;
use strict;

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

my $admin_mail = “you\”;
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″>
<td width=”69″><div align=”right”>Name:</div></td>
<td width=”234″><input type=”text” name=”name” size=”50″></td>
<td><div align=”right”>Email:</div></td>
<td><input type=”text” name=”email” size=”50″></td>
<td><div align=”right”>Message:</div></td>
<td><textarea name=”message” cols=”43″></textarea></td>
<td colspan=”2″><div align=”center”>
<input type=”submit” name=”submit” value=”submit”>


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