#!/usr/bin/perl
# Enter the location of sendmail.  
$mailprogram = "/usr/sbin/sendmail -t";

# Enter the fields that are required.  They should each be in quotes and
# separated by a comma.  If no fields are required, change the next line
# to @required = ();
@required = ('email','subject');

# Enter your e-mail address.  Be sure to put a \ in front of the @.
# (user@domain.com becomes user\@domain.com)
$youremail = "nesbitt\@hawaii.edu";

# Put the posted data into variables

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        $FORM{$name} = $value;
}

# Check for all required fields

foreach $check(@required) {
        unless ($FORM{$check}) {
                print "Content-type: text/html\n\n";
                print "<html><head><title>Wrong input</title></head>\n";
                print "<body><h1>Wrong input</h1><br>\n";
                print "Sorry you forgot\n";
                print "to fill in the $check field!\n";
                print "turnback and try again\n";
                print "</body></html>\n";
                exit;
        }
}

# Check the senders email

if ($FORM{'email'}) {
        unless ($FORM{'email'} =~ /\w+@\w+.\w+/) {
                print "Content-type: text/html\n\n";
                print "<html><head><title>Wrong e-mail address</title></head>\n";
                print "<body><h1>Wrong e-mail adres</h1><br>The e-mail address, \n";
                print "$FORM{'email'}, is not valid.  turn back to the  form\n";
                print "and try again\n";
                exit;
        }
}

open (MAIL,"|$mailprogram");
print MAIL "To: $youremail\n";
print MAIL "From: $FORM{'email'}\n";
print MAIL "Subject: $FORM{'subject'}\n";
print MAIL "Auto-responder message.\n";
foreach $pair (@pairs) {
        ($name, $value) = split(/=/, $pair);
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        unless ($name eq "response" || $name eq "email" || $name eq "subject") { 
                print MAIL "$name: $value\n";
        }
}
close MAIL;

if ($FORM{'response'} && $FORM{'email'}) {
        open (RESPONSE, $FORM{'response'});
        @response = <RESPONSE>;
        close(RESPONSE);
        open (MAIL,"|$mailprogram");
        print MAIL "To: $FORM{'email'}\n";
        print MAIL "From: $youremail\n";
        print MAIL "Subject: $FORM{'subject'} -- Autoresponse\n";
        foreach $line (@response) {
                print MAIL "$line";
        }
        print MAIL "\n";
        print MAIL "\n";
        print MAIL "\n";
        print MAIL "\n";
        close MAIL;
}

print "Content-type: text/html\n\n";
print "<html><head><title>Thank You!</title></head>\n";
print "<body><h1>thanks!</h1><br>\n";
print"<body><h3> thanks</h3><br>\n";
if ($FORM{'response'} && $FORM{'email'}) {
        print "this form has sent an autoreply to your email address<p>\n";
}
print "Have a nice day.\n";


