###############################################################################
#                       MAIL_LIB.PL
#
# This script was written by Gunther Birznieks.
# This script has been heavily modified by Michael E. Schechter
# Date Created: 2-22-96
# Date Last Modified: 11-18-98
#
#   You may copy this under the terms of the GNU General Public
#   License or the Artistic License which is distributed with
#   copies of Perl v5.x for UNIX.
#
# Purpose: Provides a set of library routines to send email
# over the internet.  
#
# Main Procedures:
#  send_mail - easier to use version of send_mail
#  web_error - outputs an error message if the sendmail program is not launched.
#
# Special Notes: Script is UNIX Specific and ties into the sendmail Program
# which is usually located in /usr/lib or /usr/bin.
# 
# Also, remember to escape @ signs with a backslash (\@) 
# for compatibility with PERL 5.
#
# Change the $mail_program variable to change location of your
# sendmail program
#
###############################################################################

###############################################################################
# Subroutine:  &send_mail ()
#
#	Parameters:
#		$fromuser    = Full Email address of sender
#		$touser      = Full Email address of receiver
#		$subject     = Subject of message
#		$messagebody = Body of message including newlines.
#
#   Usage:
#		&send_mail("me@myhouse.com","you@yourhouse.com",
#				   "My subject","My message");
#
#   Output:
#		None, if sendmail is found and started correctly.  Error message
#		if sendmail is incorrectly called.
#
#	Comments:
#		The -t flag in the sendmail call tells sendmail to
#		look for To:, From:, Subject: header lines in the
#		mail message to determine the addresses to send to.
#		This is a security measure and should ALWAYS be used.
#
#		The -n flag in the sendmail call tells sendmail not to
#		use the alias list for the UNIX server.  We do not want
#		outsiders using	the alias list of the webserver in most cases.
###############################################################################

sub send_mail {
    local($fromuser, $touser, $subject, $messagebody) = @_;
	local($mail_program) = '/usr/lib/sendmail -t -n';
	local($to_mail);
    open (MAIL, "|$mail_program") || &web_error("Could Not Open Mail Program");
	$to_mail = "To: $touser\n";
	$to_mail .= "From: $fromuser\n";
	$to_mail .= "Subject: $subject\n\n";
	$to_mail .= "$messagebody\n\n";
	print MAIL $to_mail;
    close (MAIL);
}

###############################################################################
# Subroutine:  &web_error ()
#
#	Parameters:
#		$error = Description of Web Error.
#
#   Usage:
#		&web_error("My Error Message");
#
#   Output:
#		Error message.
###############################################################################

sub web_error {
	local ($error) = @_;
	$error = "Error Occured: $error";
	print "$error<p>\n";
    die $error;
}

1;

