#!/usr/bin/perl
######################################################################
# AMProSoft Form Mailer           Version  1.00                      #
# Begun: 11/09/2000               Completed 11/10/00                 #
# Last Revision: none             http://www.AMProSoft.com/software  #
# Copyright AMProSoft 2000        Permision granted to modify        #
#====================================================================#
#  Any modifications to this script must be made in accordance       #
#  with the Terms and Conditions located on the web at               #
#  http://www.AMProSoft.com/software/FormMailer                      #
#  installation instructions can also be found at that address       #
#======(please document any modifications below this line)===========#
#====================================================================#
######################################################################

# ======================================================================
# Variables 
# ======================================================================

 $MailProg    = '/usr/sbin/sendmail';   # Path to sendmail program
 $MailDest    = 'you@yourdomain.com';   # Person Whom Form should be mailed to.
 $MailSubj    = "Your form results";    # Subject Line
 @Referers    = ('http://www.yourdomain.com', 'http://yourdomain.com');    # List of Allowed Servers
 $HTMLBodyTag = "<body bgcolor=black link=white vlink=#FFBBFF alink=yellow topmargin=0 leftmargin=0>";
 $MessageFont = "<font face=verdana size=3 color=white><b>";
 $ThanksMsg   = "Thank you for your submission.<br><br><a href=\"/\">Click here to return home</a>.<br><br>";

# ======================================================================
# Main Program
# ======================================================================

 &CheckReferers; 
 %InFormData = ();
 $AFMBody = "";

 &ParseData;

 $AFMFrom = $InFormData{'EMail'};
 $AFMTo = $MailDest;
 $AFMSubject = $MailSubj; 

 &SendMail;
 &ThanksPage;
 exit;

# ======================================================================
# Subroutines
# ======================================================================

sub CheckReferers {

  if (defined $ENV{'HTTP_REFERER'}) {
    foreach $Referer (@Referers) {
      if ($ENV{'HTTP_REFERER'} =~ /^$Referer/i) { return; }
    }
  } 
  $MsgTitle = "unauthorized Access";
  $Message = "Unauthorized access<br><br>Attemp to access this program from an unauthorized domain.<br>\n";
  &PrintMessage;
  exit;

}

sub ParseData {
  $InputString = "";
  @Pairs = ();

  read (STDIN, $InputString, $ENV{'CONTENT_LENGTH'});
  $InputString =~ s/</&lt;/gi;  
  @Pairs = split(/&/, $InputString);

  foreach $Item (@Pairs) {
    ($Name, $Value) = split(/=/, $Item);
    $Value =~ tr/+/ /;
    $Value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $AFMBody .= "\n " . $Name . " = " . $Value . "<br>";
    $InFormData{$Name} = $Value;
  }
}

sub SendMail {

  open(MAIL,"|$MailProg -t");
  print MAIL "To: $AFMTo\n";
  print MAIL "From: $AFMFrom\n";
  print MAIL "Subject: $AFMSubject\n\n";
  print MAIL "$AFMBody\n\n";
  close (MAIL);

}

sub ThanksPage {

  $MsgTitle = "Thank you";
  $Message = "$ThanksMsg<br><br>";
  $Message .= "</center><br><br>$AFMBody<br><br><center>";
  &PrintMessage;

}

sub PrintMessage {

  print "Content-type: text/html\n\n";
  print "<html><head><title>$MsgTitle</title>\n";
  print "$HTMLBodyTag<center><br><br>\n";
  print "$MessageFont $Message";
  print "<br><br><br><br></b><font size=1><a href=\"http://www.AMProSoft.com/software/FormMailer\">AMProSoft Form Mailer version 1.0</a><br><br>";
  print "</body></html>\n";

}