#!/usr/bin/perl
###################################################################
## Form-To-EMail Version 1.4 - 07/22/98                          ##
## ------------------------------------------------------------- ##
## Copyright (C) 1998 Dimension's CGI Workshop                   ##
## http://www.thenetnow.com/dimension                            ##
## dimension@cybered.net                                         ##
## ------------------------------------------------------------- ##
## This script is freeware and may not be sold in any way.  If   ##
## you would like to make changes to this script, please EMail   ##
## me and ask for my permission before doing so.  Once recieving ##
## my permission, this notice must remain in place.              ##
###################################################################

###############################################
### ****** CUSTOMIZE THESE VARIABLES ****** ###
###############################################
# Name (and location if necessary) of the server's mail program
$mailprog = '/usr/lib/sendmail';

# gbn: instead, the file to append to.  This might need a full pathname:
# IMPORTANT!  This file needs to be writable by the Web server.
# In Unix, you can "chmod 666" the file.
#$mailprog = '/export/home/gbnewby/public_html/forms/forms-output.txt';

################################################

###############################################
### ***** DO NOT EDIT BELOW THIS LINE ***** ###
###############################################

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $name =~ tr/+/ /;
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $name =~ s/<!--(.|\n)*-->//g;
   $name =~ s/<([^>]|\n)*>//g;
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;
   # gbn: not sure why < is omitted...put it back:
   #   $value =~ s/<([^>]|\n)*>//g;
   $FORM{$name} = $value;
}

# READ HIDDEN VARIABLES FROM FORM

$subject = $FORM{subject};
$to = $FORM{to};
$from = $FORM{from};
$followupurl = $FORM{followupurl};

# Open the mail program
open(MAIL,"|$mailprog -t");
# gbn: change this to a file.  Open it in append mode:
#open(MAIL, ">>$mailprog") || die "\n<font color=\"red\">Server error.  Please email the site administrator so this can be fixed.</font>\n\n";

# gbn: ditch header materials
# Send the mail program the EMail message
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n";
print MAIL "\n";
print MAIL "---------------------------------------------------------------------\n";

foreach $key (sort keys(%FORM)) {
   if ($key ne "subject" && $key ne "to" && $key ne "from" && $key ne "followupurl") {
      print MAIL "$key: $FORM{$key}\n";
   }
}

#print MAIL "---------------------------------------------------------------------\n";
#print MAIL "Form-To-EMail version 1.4\n";
#print MAIL "(C) Dimension's CGI Workshop - http://www.thenetnow.com/dimension/\n";

# Close the mail program
close(MAIL);

# Forward to the Follow-up URL
print "Location: $followupurl\n\n";
