#--------------------------------------------------------------------------
# Sends an e-mail using the Net::SMTP module.
#
# Receives:
# $From = address which will appear in the "From:" line.
# $To = address to send the message to
# $MessageRef = reference to an array that contains the complete
# message, both header and body.
#
# Example:
# SendMail ("me\@podunk.edu", "you\@somewhere.com",
# \@MessageArray);
#
# The global variable $MailServer must contain the domain name
# of the mail server to be used (e.g. mail.podunk.edu).
#
# Make sure you have the statement 'use Net::SMTP;' at the beginning of
# your script.
#
# If one of the SMTP calls returns an error code, the message is printed
# to standard output, along with the message itself.
#
# To get the above output always, set $DEBUG = 1 below; set it to 0 to
# turn this off.
#--------------------------------------------------------------------------
sub SendMail {
my ($From, $To, $MessageRef) = @_;
my $DEBUG = 0;
my $MailConnection = Net::SMTP->new($MailServer)
or die "Can't connect to mail server $MailServer!\n";
$MailConnection->mail($From)
or die "Can't start message from $From\n";
if ($MailConnection->to($To)) {
$MailConnection->data();
$MailConnection->datasend (@$MessageRef);
$MailConnection->dataend();
} else {
print "Can't start message to $To!\n";
}
if ($MailConnection->ok()) {
$DEBUG && print "\nResult from mail server:\n";
$DEBUG && print $MailConnection->code();
$DEBUG && print " ";
$DEBUG && print $MailConnection->message();
$DEBUG && print "\n";
$DEBUG && print "---------- message -----------\n";
$DEBUG && print @$MessageRef;
} else {
print "\nMail failed! Response from mail server:\n";
print $MailConnection->code();
print " ";
print $MailConnection->message();
print "\n";
print "---------- attempted message -----------\n";
print @$MessageRef;
}
$MailConnection->quit();
}