HTML Goodies: PERL Primer, Part 4

By Joe Burns

...use these to jump around or read it all
[Our Variables]
[The Email Module]
[Let's Build It]
[Assignment]

     Now we get into the fun part. Plus, now we're far enough along that you can start to alter the script yourself. In fact, I'll give you your first chance to alter the script in your first assignment at the end of the Primer.

     Did you know there would be a test?


Our Variables

     At this point, we have four pieces of information set to scalar variables. They are:

  • $FORM{'name'}
         This is the value written into the first text box.
  • $FORM{'email'}
         This is the value written into the second text box.
  • $FORM{'submitaddress'}
         This is the email address where the form should be sent. It's the hidden input element. You didn't forget about this one, did you?
  • $FORM{'feedback'}
         This is the text that was written into the textarea box.


The Email Module

     Now, believe it or not, this stuff starts to become fun. Let's write an email letter. Here's the module of code that writes the letter taken from the Guestbook script.

open (MESSAGE,"| /usr/lib/sendmail -t");

print MESSAGE "To: $FORM{submitaddress}\n";
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To: $FORM{email}\n";

print MESSAGE "Subject: Feedback from $FORM{name} at $ENV{'REMOTE_HOST'}\n\n";
print MESSAGE "The user wrote:\n\n";
print MESSAGE "$FORM{feedback}\n";
close (MESSAGE);

&thank_you;

     We begin by using the command "open" to open a process. "Open" will also you to open a file, read, or append to a file, but we need to open a process right now.

     Look at the format, inside the parentheses are two pieces of information separated by a pipe (|). The first text "MESSAGE" will act as the "filehandle" that will represent the file we're about to write. I chose to call the file MESSAGE. You can change it to whatever you want as long as you make it a unique identifier word representing on the file you're going to create.

     The text "| /usr/lib/sendmail -t" explains what we're going to do with this file named MESSAGE. We're going to send it to the server's mail program called "sendmail".

     What I have written here is the most common path to a server mail program. Your server may be different. If so, change that here. We'll be sending it as text.

Commands & Operators

     In building the email message, we'll need to follow some rules and learn some new commands and an operator:

  • print: This does just what it says. It prints to the page. Note the format. The text is included in double quotes and each line ends with a semi-colon.

  • close: This will close MESSAGE when we're done writing it:
  • \n: This backslash "n" means "new line" If you want double spacing, use two: \n\n.

     There's nothing to it now; let's build it.


Let's Build It

     I wrote this part of the code simply by opening my Eudora email program and listing the elements it asks for, in the order it asks for them. I followed the format of spelling and capitalization.

     Remember that in the print command, the scalar variables get stuck in there as if they were the text you want because once this script runs, they will be. Here's the email header:

print MESSAGE "To: $FORM{submitaddress}\n";
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To: $FORM{email}\n";

     We print to the MESSAGE "To: $FORM{submitaddress}" and then skip a line. Note again, the line ends with a semi-colon. There's no way around this rule. Every line ends with a semi-colon.

     You can see what happens: the "submitaddress" value, which is your email address, gets stuck in there when the script rolls.

     The next line is the name the person entered and the next is the reply address the person entered into the text box that asked for his or her email box.

     This isn't so hard. Let's build the body of the email:

print MESSAGE "Subject: Feedback from $FORM{name} at $ENV{'REMOTE_HOST'}\n\n";
print MESSAGE "The user wrote:\n\n";
print MESSAGE "$FORM{feedback}\n";

     The first line creates the subject telling you that you have feedback from whatever the person entered into the NAME textbox.

     Next I used a little PERL shorthand to grab the name of the server sending the mail. Remember that $ENV represents all the data from the form? We used it last time to pluck out the request method. Well, here I'm using it again to pull out the remote host. It's a quick way of adding where the letter came from.

     We end with two lines.

     The final two lines simply write a quick intro, "The user wrote", skips two lines and then writes the value that was found within the textarea box.

     Got it? Let's wrap it up.

close (MESSAGE);

&thank_you;

     We close MESSAGE because we're done with it. Then something new. See that &thank_you?

     That's what's known as "calling for a subroutine". If, in the script, you look just below the code we are working on here, you'll see the line:

sub thank_you

     All of the text that follows that line will be used to write the thank you page. In PERL lingo, that code is called a subroutine. It's a lot like a function in JavaScript. The final line of the module that writes the email letter calls on that subroutine. It acts as a trigger.

     This keeps everything in order. First we grab and delineate the form data, then we write and send the email, lastly we trigger the subroutine that writes the thank-you page.

     It's very clean, very simple, and a little easier to understand than the last primer.

     We'll get into that thank_you subroutine and talk a little about how to require modules in PERL in Primer Five.

     Now, put your books away. It's time for your assignment.


Primer Four Assignment

     Can you alter the email code above so that the person that submitted the form gets a copy of the email?

     If that's a little much for you, can you add another line at the bottom of the email that reads, "Thanks for writing" and then prints the submitter's name?

     Can you do both? Think it through and look at an email program for the syntax for the carbon copy.


     Here's a possible answer. This will open in a new window.

PERL Primer Four - A Possible Answer

On to Primer Five...

[Our Variables]
[The Email Module]
[Let's Build It]
[Assignment]


Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date