This is the second installment of CGI Bits & Pieces, which shows you how to parse
data submitted from a HTML form and send it as a mail to a predefined email account.
If you are a CGI novice,
see the references at the end of this article. Having read
Brent's article is the bare minimum to understand anything that follows.
All examples are implemented in C++ and usually compiled and tested on both
Windows NT4 using Visual C++ 5.0 and FreeBSD/linux using egcs.
The data a user inputs into the fields of a form is sent to the URL that
is contained in the ACTION property of the FORM tag. But you do not get the data
as it is entered, but in a coded form that allows you to dissect control information from
user data. The control information is the name of the form fields, so you are able to
associate the data with the field it was entered into.
The fields are sent as a string of name=value pairs, and if there are several
fields, those pairs are separated by an ampersand "&". Certain chars in the name and value part
are www-url-encoded, which means spaces are sent as a "+" character, and all other special
characters are sent as a per-cent sign "%" followed by the code of the character as two hex-digits.
Obviously, the characters "%&+=" have to be encoded in that form; which characters are encoded exactly
is the decision of the browser.
The following code echos this encoded form information,
in clear text form, into a mail. Additionally, relevant information from the cgi-bin environment
is also included in the mail. To prevent people from sending to any email address
(i.e. using our server to spam), we have a config file that defines valid destination addresses; this also allows
us to define a follow-up URL that is showed to the user after he clicked on the submit button.
Both these configuration values are accessed by an alias name that is appended to the URL in the form tag and thus
can be found in the QUERY_STRING variable.
Below, you find the HTML code of a sample form, the C++ source code and a sample configuration file.
Finally, you can see a mail generated by the program compiled for RedHat Linux 2.0.36 using
egcs-2.91.60.
A sample FORM
wwwmail.cpp
wwwmail.cfg
A sample mail
Gundavaram, Shishir,CGI Programming on the World Wide Web.
O'Reilly, 1996, 433 p., covers CGI basics as well as hot topics like data base interfacing, details of the HTTP protocol and their use, forms, server side includes, hyper-media, client-pull and server-push (examples in Perl), ISBN 1-56592-168-2.
Hermann, Jürgen,Sending image files via CGI.
C-Scene, 1998.
York, Brent,CGI in C - A starter's tutorial.
C-Scene, 1997.