#!/usr/local/bin/perl -- -*- C -*-
#
# mailme.pl - a script which sends an email message to the owner of this file
#	   should be placed in ~user/WWW/cgi-bin and called with a URL like
#	   http://www.pcug.org.au/cgi-bin/cgiwrap/~user/mailme.pl
#	   as the form handler
# See mailme.html for a sample form that uses this script
#
# It expects the following fields to be present in the data from the client:
#    sender	- a valid fully qualified email address of the sender
#    recip	- MUST be the login name of the owner of this script
#    subject	- subject of the mail message (optional)
#    title	- title of the response sent to the client (optional)
#    arbitrary other fields and values	- placed in body of email
#
# It returns a thankyou to the caller and mails the field names and values
# supplied to the script owner with the specified subject.
# Title will be used as the title for the response page.
#
# Orignally written by Karl Auer 1 June 95
# Modified for general user use with cgiwrap by Lawrie Brown - Dec 95

# need cgi-lib.pl for the following CGI utility routines:
#	&ReadParse, &PrintHeader, &DumpVariablesShort, &SendMail
require "cgi-lib.pl";
 
# find out who we're going to mail to
$to = (getpwuid($<))[0];     # get username for this process real uid

# now read key=value pairs from form into hash table %in
&ReadParse(*in);

# deal with a dud form...
$error  = "";	# no errors yet, unless ...
$error .= "No sender email provided.<br>\n" if (!defined($in{'sender'}));
$error .= "No recipient login provided.<br>\n" if (!defined($in{'recip'}));

# grab the ones we need now
$sender = $in{'sender'};
$recip = $in{'recip'};
$subject = $in{'subject'};
$title = $in{'title'};

# throw them away so we don't send them as data
delete($in{'sender'});
delete($in{'recip'});
delete($in{'subject'});
delete($in{'title'});

# put in defaults for optional variables
$subject = "Email from Web server"	if $subject =~ /^\s*$/;
$title   = "Email Sent"			if $title   =~ /^\s*$/;

# now do some sanity checks on the data
$error .= "Invalid sender email address ($sender), " .
		"please supply a fully qualified address.<br>\n"
	unless &IsValidEmailAddress($sender);
$error .= "Recipient login ($recip) does not match owner of this script, " .
		"please contact the script owner about this.<br>\n"
	unless $recip eq $to;
$error .= "Invalid subject ($subject), please re-enter it.<br>\n"
	if $subject =~ /[\r\n]/;	# single line subject only

# collect the remaining material as a single message
$msg = &DumpVariablesShort(%in);

# For our own protection, reject overly large messages
$error .= "Too much data as been supplied!<br>\n" if (length($msg) > 10240);

# Print out a content-type for HTTP/1.0 compatibility
print &PrintHeader;
print "<html>\n";
print "<head>\n";
print "<title>$title</title>\n";
print "</head>\n";
print "<body>\n";
print "<h1>$title</h1>\n";

# now check for any errors and respond if any
if (length($error) > 0) {
   print "<h2>Ooops!</h2>\n";
   print "There is a problem with the information supplied:\n";
   print "<p>\n";
   print $error;
   print "<p>\n";
   print "Your input to the form has been discarded. Sorry about that.\n";
   print "You should go back and check that you have completed the form.\n";
   print "If the problem persists you may need to advise the form's author.\n";
   print "<p>\n";
} elsif (length($msg) == 0) {
   # no message to send, so let user know
   print "<h2>Ooops!</h2>\n";
   print "You haven't actually provided any information to send!\n";
   print "Please go back and check that you have completed the form.\n";
} else {
   # otherwise let SendMail actually send the message
   &SendMail($sender, $to, $subject, $msg);
   # and let user know whats happened
   print "<center><h2>Thank you!</h2></center>\n";
   print "The following has been mailed to $to:\n<pre>\n";
   print "$msg\n";
   print "</pre>\n";
}

# send rest of HTML header in response
print "</body>\n";
print "</html>\n";
exit(0);

