#!/usr/bin/perl -wT # # mail-form cgi # Quick and dirty Web form processing script. Emails the data # to the recipient. # # You should change any variable that has "CONFIGURE" in the # comment. #### modules (these don't need changing) # # be strict, define all variables use strict; # use CGI.pm for parsing form data and printing headers/footers use CGI; # redirect errors to the browser window use CGI::Carp qw(warningsToBrowser fatalsToBrowser); # create an instance of CGI.pm: my($cgi) = CGI->new; # CONFIGURE: change this to the proper location of sendmail on your system: my($mailprog) = '/usr/sbin/sendmail'; # Set the path, so taint mode won't complain. This should be the path # that sendmail lives in! (If you're using /usr/lib/sendmail, then # the path had better be /usr/lib here.) $ENV{PATH} = "/usr/sbin"; # CONFIGURE: change this to the e-mail address you want to receive the # mailed form data my($recipient) = 'nullbox@cgi101.com'; # CONFIGURE: change this to your URL: my($homepage) = "http://www.cgi101.com/"; # CONFIGURE: change this to the subject you want the e-mail to have # WARNING: Do NOT Let the form specify the subject line, or your # program can be hijacked by spammers!!! my($subj) = "Form Data"; # everything below this shouldn't need changing, except possibly the # "thank you" page at the end. # Print out a content-type header print $cgi->header; # CGI.pm automatically parses the data, and you can retrieve it using # $cgi->param("fieldname") # Now send mail to $recipient open (MAIL, "|$mailprog -t") || &dienice("Can't open $mailprog!\n"); print MAIL "To: $recipient\n"; print MAIL "From: $recipient\n"; print MAIL "Subject: $subj\n\n"; # print all of the form fields: foreach my $i ($cgi->param()) { print MAIL $i . ": " . $cgi->param($i) . "\n"; } print MAIL "\n\n"; # print some extra info: print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n"; print MAIL "HTTP From: $ENV{'HTTP_FROM'}\n"; print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n"; print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n"; close (MAIL); # CGI.pm prints the HTML header print $cgi->start_html(-title=>"Thank You", -bgcolor=>"#ffffff", -text=>"#000000"); # CONFIGURE: # Print a thank-you page - you can customize this however you wish. # Be sure to use proper HTML, and escape any $-signs or @-signs with a # backslash, e.g.: \$25.00, nullbox\@cgi101.com print <Thank you for writing!

Thank you for your feedback. Return to our home page.

EndHTML # CGI.pm prints the HTML footer print $cgi->end_html; # error handler sub dienice { my($msg) = @_; print $cgi->start_html(-title=>"Error"); print qq(

Error

\n); print $msg; print $cgi->end_html; exit; } # the end.