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.

  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


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:
<style type=text/css>
td {font-family: Arial}
<h2>So Who Are You, Anyway?</h2>
<form action="mailform.cgi" method=post>
<table border="0" cellspacing="5" cellpadding="0">
<td><input type=text name=who size=30></td>
<td>Email address</td>
<td><input type=text name=email size=30></td>
<td valign="top">Address</td>
<td><textarea name="address" cols="30" rows="5"></textarea></td>
<td><input type=text name=age size=2></td>
<td align=center colspan=2><input type=submit value=Send!>  <input

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
# assign values to the variable depending on form METHOD
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

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