Perl
  Home arrow Perl arrow Page 4 - Perl 101: The email form
The Best Selling PC Migration Utility.
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: The email form
By: Pete Smith
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      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

    PCmover - $15 Off with Coupon Code CJPH7Q

    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

    - 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 2 hosted by Hostway