#!/usr/bin/perl -w
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
  @pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
  read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  @pairs = split(/&/, $buffer);
}
foreach $pair (@pairs) {
  local($name, $value) = split(/=/, $pair);
  $name =~ tr/+/ /;
  $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ tr/+/ /;
  $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  $value =~ s/~!/ ~!/g;
  if (substr($name,0,6) eq 'email_') {
    push(@AllVal,$name);
  }
  $$name = $value;
}
$httphost = $ENV{'HTTP_HOST'};

open (SENDMAIL, "|/usr/sbin/sendmail -t") or die("uhg");
print SENDMAIL <<"EOF";
From: Form data from $httphost <$toname\@$httphost>
Subject: $subject
To: $toname <$toname\@$httphost>
MIME-Version: 1.0
Content-Type: TEXT/HTML; charset=US-ASCII

<html>
 <body>
  <table border=0 cellpadding=0 cellspacing=0>
EOF
foreach $nv (@AllVal) {
  print SENDMAIL "   <tr>\n";
  print SENDMAIL "    <td>\n";
  print SENDMAIL "     ".$nv.":\n";
  print SENDMAIL "    </td>\n";
  print SENDMAIL "    <td>\n";
  print SENDMAIL "     ".$$nv."\n";
  print SENDMAIL "    </td>\n";
  print SENDMAIL "   </tr>\n";
}
print SENDMAIL <<"EOF";
  </table>
 </body>
</html>
EOF
close (SENDMAIL);
print "Location: $goto\n\n";
exit;
