"How to Build a List Using CGI"

by Steve Humphrey

The purest form of opt-in list is the electronic newsletter or "e-zine." You ask people to sign up to get the newsletter, then you send them information about a topic that interests them.

Let's look at how CGI can help you to sign them up. Assume that a person can join your list by sending an e-mail message to a special address. Most lists work that way.

You put a element on a page and use a CGI script to deal with the info collected by the form.

People type (or paste) in their address, maybe even their name (if you ask for it). When they click the "Subscribe" button, CGI sends a message to the server with all the data attached. A CGI program on the server then acts on it.

The script will need just a few parts:

  1. Code to extract the data (Name, E-mail address, etc.)
  2. Code to reject obviously bad addresses [Optional].
  3. Code to build and send the e-mail message to start the subscription.
  4. Some type of output. CGI doesn't like to end with no output; the script usually fails to work!

For the first step, you can use a subroutine from my book or from almost any script you can read. Just look at the part where "name and value" pairs are being separated. Lines like this: $Name = $val; mean you are copying the current value into a local variable for later use.

The easiest way to handle the name/value pairs is to use a function (or "method") from the CGI.pm module. Just add these lines to the top of the script (after the path line, of course):

use CGI;
$query = new CGI; OR
$query = CGI::new();

This creates a "CGI object," which contains all the data from the form. You can pull out the items you want with one line of code each, like this:

$First_Name = $query->param("fname");
$Last_Name = $query->param("lname");
$Email = $query->param("email");

Second, after the e-mail address has been copied to a local variable, you can run some tests on $email and try to determine if it's any good. These tests are done by a comparison or match statement. The match operator (=~) is used to see if a pattern exists in a given string of characters. For example, the match statement:

$email =~ /getresponse.com/
is TRUE if $email is an address at getresponse.com

Note: The "@" character has a special meaning. Using the backslash tells the program to treat it as an ordinary character instead.

It's a good idea to check to see whether it even looks like an e-mail address at all. This is more involved than you might think. Your brain can figure it out at a glance, while a script has to check the pattern very carefully. Here's the general way an e-mail address should "look" in order to be valid:

/^[\w\d][\w\d\,\.\-]*\@([\w\d\-]+\.)+([a-zA-Z]{3}|[a-zA-Z]{2})$/

OK, it looks pretty weird but it's really just a "regular expression" that defines the pattern of a valid e-mail address. Perl can compare any alleged e-mail address against this pattern. If it fails to match this, it's simply not an e-mail address at all.

The subroutine "error" might display an error page or just return the user to the home page, etc.

The third piece of the puzzle is having your server send the e-mail message. How this code is written depends on the server your Web site is on and what mail program it uses. You could look at a script that you know is working on the server (maybe one your host gave you) and see if it has an e-mailing subroutine. Recycle good code!

You can get even more mileage out of the script by making it send a pre-determined message to the new subscriber. Just write a little subroutine that composes the message you want to send and, at the end of this subroutine, call the e-mail sending subroutine.

There's usually a variable that represents either the whole message or just the body. You just add line after line to it like this (to fill in the message body):

$body = "This is the first line of the message\n";
$body .= "This is the next line of the message\n";
.
.
.
$body .= "This is the last line of the message\n";

Finally, the script should output something to send back to the user. This can be as simple as returning the user to the home page, or as complex as sending a customized "Thank-You" page. This would be a good time to offer the user a gift for subscribing. It could be a free e-book, etc.

Once you have the script in place, test it to make sure it does exactly what you expect. Subscribe yourself to the list and look at any canned message that gets sent. Go back and correct any typos, etc., in the message. Unless you type a lot better than I do, there will probably be at least one.

Well, what's left? You've got a fully automated process for signing up new subscribers to your list. You'll reject the addresses you don't want to mail to (autoresponders, etc.). All you need now is for people to use the process.

Give them a good reason to get mail from you. It could be valuable information, tips, techniques, etc. It could be a free ad in your newsletter or a chance to enter a contest and win a prize. Determine what will motivate your people.

One thing that really works well is to offer some sort of incentive to your current subscribers who will bring their friends into your list. Try something. If it works, great. If not, try something else.


Steve Humphrey promises that you can learn to use CGI to turn your own Web site into a marketing machine in two hours or less with his excellent CGI learning system: "Learn to Use CGI in 2 Hours."

Required reading for anyone who wants to automate their Web site or their marketing efforts: Click here for more...




Unix Tutorials ~ Unix System Security ~ Unix Help
Free URL Submit ~ UnixTools.com Home ~ Free Web Resources
Unix Software ~ Unix Hardware ~ Web Related Books

  1. Please email additional sites, redirected/broken link info,
    suggestions & questions to: webmaster@unixtools.com

  2. Free Computer-Related Freebies - Free Software,
    ISP access, email, webmaster goodies, and fun stuff.

  3. Search for books on CGI at Amazon.com
  4. Get Expert Tips and Tricks for Promoting Your Business Online

Pages are © Copyright 1998-2003 by UnixTools.com