#!/usr/bin/perl
##
## Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
## Creation Date: Tue Jul 20 14:58:50 PDT 1999
## Last Modified: Tue Jul 20 14:58:53 PDT 1999
## Filename:      /home/httpd/cgi-bin/mailtest
## Syntax:        Perl 5; CGI
## $Smake:        cp -f %f /home/httpd/cgi-bin/%b
##
## Description:   This program demonstrates the use of CGI::* module
## 		  using CGI::Form
##

use CGI::Form;
$cgi_form = new CGI::Form;

$from    = $cgi_form->param('from');
$name    = $cgi_form->param('name');
$to      = $cgi_form->param('to');
$subject = $cgi_form->param('subject');
$message = $cgi_form->param('message');

# remove DOS linefeeds if present:
$message =~ s///g;


###########################################################################
#
# Send a reply to the user who submitted the message to send
#

print <<"EOT";
Content-type: text/html

<html>
<head>
<title>Mail test results</title>
</head>
<body bgcolor=#eeeeee>
<h1> Mail test results <hr noshade> </h1>
<ul>
EOT

if ($to ne "") {
   print "<p> The contents of the sent message was: \n";
} else {
   print "<p> Error: message not sent since no recipient.  Message was: \n";
}

print <<"EOT";
<p>
<table width=80% bgcolor=#661166 cellspacing=3 cellpadding=3> <tr><td>
<table width=100% bgcolor=#c8bbf8 cellspacing=8 cellpadding=15> <tr><td>
<pre>
<b>From:</b>     anonymous\@hummer.stanford.edu
EOT

if ($from ne "") {
   if ($name eq "") {
      print "<b>Reply-To:</b> $from\n"
   } else {
      print "<b>Reply-To:</b> $name &lt;$from&gt;\n"
   }
}

print <<"EOT";
<b>To:</b>       $to
<b>Subject:</b>  $subject
</td></tr>
<tr><td>
<pre>
$message
</pre>
</td></tr></table>
</td></tr>
</table>
</ul>
</body>
</html>
EOT


###########################################################################
#
# Send the mail message
#

open(SENDMAIL, "| /var/qmail/bin/qmail-inject");
## or if using sendmail:
# open(SENDMAIL, "| /usr/bin/sendmail -t -n");


# if ($to ne "" && length($message) < 80) {
if ($to ne "") {
   if ($from ne "") {
      if ($name eq "") {
         print SENDMAIL "Reply-To: $from\n";
      } else {
         print SENDMAIL "Reply-To: $name <$from>\n";
      }
   
      print SENDMAIL <<"EOM";
To: $to
Subject: $subject

$message
EOM

   }
}


###########################################################################
#
# log the mail message to mailtest.log in case someone is hacking
# this CGI program.
#

open(LOGFILE, ">> /var/log/httpd/cgi/mailtest.log");

print LOGFILE "\n\n++++++++++++++++++++++++++++++++++++++++++++++++++\n";
if ($to eq "") {
   print LOGFILE "Failed message\n";
}
print LOGFILE "HTTP_FROM:    $ENV{'HTTP_FROM'}\n";
print LOGFILE "REMOTE_USER:  $ENV{'REMOTE_USER'}\n";
print LOGFILE "REMOTE_IDENT: $ENV{'REMOTE_IDENT'}\n";
print LOGFILE "REMOTE_HOST:  $ENV{'REMOTE_HOST'}\n";
print LOGFILE "REMOTE_ADDR:  $ENV{'REMOTE_ADDR'}\n";

print LOGFILE "Message character count: ", length($message), "\n";

if ($name eq "") {
   print LOGFILE "Reply-To: $from\n";
} else {
   print LOGFILE "Reply-To: $name <$from>\n";
}
   
print LOGFILE <<"EOM";
To: $to
Subject: $subject

$message
EOM




