the virtual poet![]()
by Dave Taylor 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 ScriptsThis 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 screenindex.html which looks like this:
<FORM ACTION=send-poem.cgi METHOD=post>
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]& 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 routinesget_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(); Then we save all the variables into their own space for easier reference:
strcpy(yourname, valueof("yourname", 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) sprintf(command, "/usr/sbin/sendmail -oi %s", temail);
if ((pd = popen(command, "w")) == NULL) {
fprintf(pd, "Reply-To: %s (%s)\n", yemail, yname);
fprintf(pd, "%s has a poem for you:\n\n", yname);
fprintf(pd, "\n\nThis poem brought to you by The Virtual
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.
|
![]() First posted: 3rd December 1997 efc Last changed: 3rd December 1997 efc |
|