Perl
  Home arrow Perl arrow Page 8 - Perl 101 (part 8) - Putting It To The ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (part 8) - Putting It To The Test
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2000-10-02

    Table of Contents:
  • Perl 101 (part 8) - Putting It To The Test
  • Adding Things Up
  • Visitors Welcome!
  • The Code...
  • ...And The Explanation
  • Going Backwards
  • Fortune Cookies
  • You Have Mail!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl 101 (part 8) - Putting It To The Test - You Have Mail!
    (Page 8 of 8 )

    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.
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway