Writing the E-mail

The fourth primer in the PERL Series builds on first three.

provided by: 
Originally published at Internet.com


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: Here's the code 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 thhat 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.

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

Author: Joe Burns

Read article at Internet.com site
Regional Articles
- Writing the E-mail Alabama
- Writing the E-mail Alaska
- Writing the E-mail Arizona
- Writing the E-mail Arkansas
- Writing the E-mail California
- Writing the E-mail Colorado
- Writing the E-mail Connecticut
- Writing the E-mail DC
- Writing the E-mail Delaware
- Writing the E-mail Florida
- Writing the E-mail Georgia
- Writing the E-mail Hawaii
- Writing the E-mail Idaho
- Writing the E-mail Illinois
- Writing the E-mail Indiana
- Writing the E-mail Iowa
- Writing the E-mail Kansas
- Writing the E-mail Kentucky
- Writing the E-mail Louisiana
- Writing the E-mail Maine
- Writing the E-mail Maryland
- Writing the E-mail Massachusetts
- Writing the E-mail Michigan
- Writing the E-mail Minnesota
- Writing the E-mail Mississippi
- Writing the E-mail Missouri
- Writing the E-mail Montana
- Writing the E-mail Nebraska
- Writing the E-mail Nevada
- Writing the E-mail New Hampshire
- Writing the E-mail New Jersey
- Writing the E-mail New Mexico
- Writing the E-mail New York
- Writing the E-mail North Carolina
- Writing the E-mail North Dakota
- Writing the E-mail Ohio
- Writing the E-mail Oklahoma
- Writing the E-mail Oregon
- Writing the E-mail Pennsylvania
- Writing the E-mail Rhode Island
- Writing the E-mail South Carolina
- Writing the E-mail South Dakota
- Writing the E-mail Tennessee
- Writing the E-mail Texas
- Writing the E-mail Utah
- Writing the E-mail Vermont
- Writing the E-mail Virginia
- Writing the E-mail Washington
- Writing the E-mail West Virginia
- Writing the E-mail Wisconsin
- Writing the E-mail Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML