Home arrow Perl Programming arrow Page 8 - Perl 101 (part 8) - Putting It To The Test

You Have Mail! - Perl

In the concluding article in the Perl 101 series, everythingyou've learnt so far is put to the test when you develop some real-worldCGI applications - a counter, a guest book and a form mailer.

TABLE OF CONTENTS:
  1. Perl 101 (part 8) - Putting It To The Test
  2. Adding Things Up
  3. Visitors Welcome!
  4. The Code...
  5. ...And The Explanation
  6. Going Backwards
  7. Fortune Cookies
  8. You Have Mail!
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 4
October 02, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
And on to our final example of the day - a simple form mailer. Let's assume that you have a feedback form which looks like this:
<html>
<head>
<style type=text/css>
td {font-family: Arial}
</style>
</head>
<body>
<h2>So Who Are You, Anyway?</h2>
<form action="mailform.cgi" method=post>
<table border="0" cellspacing="5" cellpadding="0">
<tr>
<td>Name</td>
<td><input type=text name=who size=30></td>
</tr>
<tr>
<td>Email address</td>
<td><input type=text name=email size=30></td>
</tr>
<tr>
<td valign="top">Address</td>
<td><textarea name="address" cols="30" rows="5"></textarea></td>
</tr>
<tr>
<td>Age</td>
<td><input type=text name=age size=2></td>
</tr>
<tr>
<td align=center colspan=2><input type=submit value=Send!>  <input
type=reset></td>
</tr>
</table>
</form>
</body>
</html>

Now, you need to have the contents of this form emailed to you every time someone submits it. Here's the Perl script that you'll need:
#! /usr/bin/perl
# mailform.cgi - email form contents to webmaster
# define a variable that accepts a value from the form
$in;
# assign values to the variable depending on form METHOD
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$in = $ENV{'QUERY_STRING'};
}
else {
$in = <STDIN>;
}
# fix URL-encoded strings
$in =~ s/\+/ /g;
# all variables are passed to the script as name-value pairs separated by &
# split the input string on the basis of &
@detail = split (/&/, $in);
# open mail pipe
open (MAIL,"|/usr/sbin/sendmail -t");
print MAIL "To: <webmaster\@yoursite.com>\n";
print MAIL "From: Feedback Form Mailer\n";
print MAIL "Subject: Feedback on your site\n\n";
print MAIL "Here is the result of your feedback form.\n\n";
foreach $details(@detail)
{
%details = split (/=/, $details);
while (($name, $value) = each %details)
{
print MAIL "$name: $value\n";
}
}
close MAIL;
print "Content-Type: text/html\n\n";
print "<html><body><center><font face=Arial size=+1>Thank you for your
feedback!</font></center></body></html>";

If you take a close look, you'll see that this script is very similar to the one we used when writing the guestbook. Here too, we've accepted form data as a single string, split it on the basis of the &, and then further split it against the = separator.

Next, we've opened a file handle - except that we haven't actually opened a file, but a UNIX "pipe", which allows you to "pipe" data to a UNIX command. In this case, the command is the sendmail program, which is used to deliver email. sendmail needs a few basic headers - the To:, From: and Subject: fields, which we've provided, followed by each name-value pair in the body of the message. Once all the pairs are exhausted, the handle is closed and the mail is sent out.

And here's the sample mail that you'll see:

To: webmaster@yoursite.com
From: Feedback.Form.Mailer
Subject: Feedback on your site
Here is the result of your feedback form.
who: johndoe
email: johndoe@cyberspace.com
address: the web
age: 28

And that's about it for this time. We hope you enjoyed this series of tutorials - write in and tell us what you'd like to see next. And till next time, stay healthy!

This article copyright Melonfire 2000. All rights reserved.

 
 
>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: