###############################################################################
#                       MAIL_LIB.PL
#
# This script was written by Michael E. Schechter (mschechter@earthlink.net).
# 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
#
# Special Notes: Script is Windows Specific and ties into the Blat Program
# which must be installed on the host machine.
# 
# Also, remember to escape @ signs with a backslash (\@) 
# for compatibility with PERL 5.
#
# Change the $mailprog variable to change location of your
# Blat program if it is not in your path.
#
###############################################################################

###############################################################################
# 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.
#
#	Comments:
#		Be sure to change the location of the $WIN_TEMPFILE variable.
#		This is a temporary file that will be used to store the message
#		for Blat to send out.
#
#		The -t argument tells Blat the next value is who to send the
#		email to.
#
#		The -i argument tells Blat the next value is who the message is
#		from, and to send the message even if the specified email address
#		is not found in the SMTP server list.  This is to facilitate
#		from email addresses from outside of your organization if necessary.
#
#		The -s argument tells Blat the next value is the subject to use
#		for the email message.
#
#		The -q argument tells Blat to suppress ALL status outputs.  If you are
#		having problems getting the script to execute, remove this value to
#		troubleshoot.  Just be sure to put it back when done!
#
#		The system call being split into two strings is a security measure,
#		and should be left in place.
###############################################################################

sub send_mail {
	local($from, $to, $subject, $messagebody) = @_;
	local($mail_program, $temp_file, $blat_string);
	$mail_program = "Blat";
	$temp_file = "c:/temp/tempmail.$$";
	open(MAIL, ">$temp_file");
	print MAIL "$messagebody\n";
	close MAIL;
	$temp_file =~ s/\//\\/g;
	$mail_program =~ s/\//\\/g;
	$blat_string = "$temp_file -t $to -i $from -s \"$subject\" -q";
	system ("$mail_program", "$blat_string");
	unlink $temp_file;
}

1;