Perl
  Home arrow Perl arrow Page 4 - Perl 101: The email form
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PERL

Perl 101: The email form
By: Pete Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2002-04-25


    Table of Contents:
  • Perl 101: The email form
  • Hello world in Perl
  • Refining Your Script
  • More Regular Expressions
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Perl 101: The email form - More Regular Expressions
    ( Page 4 of 5 )

    As I mentioned earlier, the regular expression we created on the previous page was pretty basic (you may well be wondering what a complex regexe looks like, but try not to for now). Now that we've covered the basics, it's time to improve on our regex.

    unless ($name =~ /^[\w ]/)
    {
    print "Oops you entered your name incorrectly - please go back and check it<br>";
    die;
    }


    This works fine for filtering the users name, but what about his email address? Email addresses contain periods and at symbols, so we'll need to allow for those. In the previous section we talked about how characters inside the square brackets are alternates. To allow @ and . we simply add them to what we already have:

    unless ($email =~ /^[\w @.]/)
    {
    print "Oops you entered your email incorrectly - please go back and check it<br>";
    die;
    }


    And for the actual message we'll again limit it to letters, number, underscores and spaces:

    unless ($message =~ /^[\w ]/)
    {
    print "Sorry, you can only use letters, numbers, underscores and spaces in your message<br>";
    die;
    }


    Putting It All Together
    So finally we have a complete script, which now looks like this:

    #!/usr/bin/perl

    use CGI;
    $q = new CGI;

    $sendmailpath = "/usr/lib/sendmail";
    $myemail = "pete\@p-smith.co.uk";
    $name = $q->param("name");
    $email = $q->param("email");
    $message = $q->param("message");

    print "Content-type:text/html\n\n";

    unless ($name =~ /^[\w ]/)
    {
    print "Oops you entered your name incorrectly - please go back and check it<br>";
    die;
    }

    unless ($email =~ /^[\w @.]/)
    {
    print "Oops you entered your email incorrectly - please go back and check it<br>";
    die;
    }

    unless ($name =~ /^[\w ]/)
    {
    print "Oops you entered your message incorrectly - please go back and check it<br>";
    die;
    }

    open(MAIL, "| $sendmailpath -t");

    print MAIL "To: $myemail";
    print MAIL "Reply: $email";
    print MAIL "Subject:Webmail $name";
    print MAIL "\n";
    print MAIL "$message";

    close MAIL;

    print "<html><body>Thank you for your comments, you mail has been sent";


    Upload it as you did earlier then go back to your hHTML form, fill it in, and submit it. If all goes well you will receive the 'Thank You' message. If not, it's time to check through your script for typos again. Wait a few minutes then check your email. You should have received the message you sent from the form.

     
     
    >>> More Perl Articles          >>> More By Pete Smith
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek