Home arrow Perl Programming arrow Page 4 - Perl 101: The email form

More Regular Expressions&toc - Perl

Never worked with Perl but always wondered how it works? In this article Pete starts from scratch, explaining everything we need to know to code, test and implement a "mail me" script with Perl. He shows us how to setup the form, how to use the CGI module, and also how to pipe commands directly to sendmail. If you've never worked with Perl before, then this article is exactly what you need to get you started!

TABLE OF CONTENTS:
  1. Perl 101: The email form
  2. Hello world in Perl
  3. Refining Your Script
  4. More Regular Expressions
  5. Conclusion
By: Pete Smith
Rating: starstarstarstarstar / 13
April 25, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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 Programming Articles          >>> More By Pete Smith
 

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: