The Web MasterUSENIX

 

the virtual poet

taylor, dave

by Dave Taylor
<[email protected]>

Dave Taylor is the president of interface design firm Intuitive Systems and author of many best-selling books on UNIX and Web page design. He's also CTO of The Internet Mall, the largest shopping site on the Net.

Email from CGI Scripts

This time I want to show you the basics of creating an area on your site that sends email based on the information entered by a user. For my example, I'm going to show you the steps involved in creating the Virtual Poet.

The concept is simple: the visitor to the site will enter his or her name, email address, and the name and email address of the recipient. Having specified that, the visitor will enter a poem, which will be mailed directly to the recipient. Of course, this is just a skeleton; otherwise you'll be asking why you wouldn't just enter the poem directly in an email message. But roll with me, eh?

Entering the Poem

Let's start with the entry screen­index.html­ which looks like this:

<FORM ACTION=send-poem.cgi METHOD=post>
Your name:
<INPUT TYPE=text NAME=yourname >
<P>
Your email address:
<INPUT TYPE=text NAME=youremail >
<P>
Recipient name:
<INPUT TYPE=text NAME=theirname >
<P>
Recipient email address:
<INPUT TYPE=text NAME=theiremail >
<P>

Type in your poem here:
<TEXTAREA NAME=poem ROWS=7 COLS=60>
</TEXTAREA>
<P>
<INPUT TYPE=submit VALUE="send your poem">
</FORM>

poems It's a basic input form with five fields; the name and email address of the sender, the name and email address of the recipient, and the poem. If there's anything interesting to note about this, it's that I don't have a NAME= area on the final submit button, which means that the button value won't be sent to the receiving script (send-poem.cgi, as specified in the ACTION clause of the FORM).

Receiving the Poetry

So far, the CGI scripts we've written have received information via a METHOD=get, but that's not going to work with this particular scenario because we might receive more data than can be included on a URL line. In any case, it's more elegant.

To receive the information, we need to read it off the data stream. To do that, I'll utilize one of a set of useful C routines I've previously written and saved as cgi-utils.c (You can download a copy and read it yourself, if you're curious.)

The basic strategy for using the CGI utilities library is to use the routine get_cgi_environment(), which returns the data stream exactly as transmitted. A typical value might look like:

yourname=Dave&[email protected]&
theirname=Dave2&[email protected]&
poem=roses+are+red+etc.

The information is packed for transmission. That's where the second routine comes in handy: cleanup() unpacks all the special encodings and lets you use the valueof() routine to extract specific values to the named variables. So you could easily have a simple line of C like:

printf("yourname = %s\n", valueof("yourname", cgienv));

Those three routines­get_cgi_environment(), cleanup(), and valueof()­are the heart of the cgi-utils library. Armed with them, we can get on to the actual program.

Reading the Environment

Here's the beginning of the actual send-poem.c program:

original_env = get_cgi_environment();
environment = cleanup(original_env);

Then we save all the variables into their own space for easier reference:

strcpy(yourname, valueof("yourname", environment));
strcpy(youremail, valueof("youremail", environment));
strcpy(theirname, valueof("theirname", environment));
strcpy(theiremail, valueof("theiremail", environment));
strcpy(poem, valueof("poem", environment));

That's the hard part. It's all an easy progression from here, much of which is codified in the routine send_notification():

send_notification(yname, yemail, tname, temail, poem)
char *yname, *yemail, *tname, *temail, *poem;
{
 FILE *pd;
 char command[SLEN];

 sprintf(command, "/usr/sbin/sendmail -oi %s", temail);

 if ((pd = popen(command, "w")) == NULL) {
  printf("Couldn't open pipe '%s'\n", command);
  exit(0);
 }

 fprintf(pd, "Reply-To: %s (%s)\n", yemail, yname);
 fprintf(pd, "To: %s (%s)\n", temail, tname);
 fprintf(pd, "Subject: A Virtual Poem from %s\n", yname);
 fprintf(pd, "\n");

 fprintf(pd, "%s has a poem for you:\n\n", yname);
 fprintf(pd, "%s\n\n", poem);

 fprintf(pd, "\n\nThis poem brought to you by The Virtual
           Poet. \n");

 pclose(pd);
}

There are some additional parts to the puzzle, of course, things that you need to get a full C program to work, but the gist of it has been presented here. With all this plugged in, the Virtual Poet site is up and ready to try.

You can visit <http://www.intuitive.com/CGI/poems/> for yourself and see what it looks like.

Expanding the Site

What would really make this cool, of course, would be to have the poem actually saved on the server and a "key" mailed to the recipient, who would then go to a specific spot on the site, enter the key, and receive a Web page that offers the poem in an attractive format. A number of virtual florists and electronic greeting card sites that do just this. It wouldn't be too hard: either you would create the Web page and save it directly when the user enters their poem, or save an intermediate data set, then parse and create the page on the fly. A little help from cron to expire files as they get more than a certain age, and you'd be ready to compete with some very expensive, carefully designed sites.

 

?Need help? Use our Contacts page.
First posted: 3rd December 1997 efc
Last changed: 3rd December 1997 efc
Issue index
;login: index
USENIX home