#!/usr/bin/env perl
#
# A notifier script that sends emails to the given addresses.
#
#   Usage: ./send-email -service <name> -msg <file> -emails email1 [email2...]
#                       -logfile <logfile> -loglevel <level> -logformat <format>
#      where the general parameters for any notifier are:
#         -service <name> idetifies a service on whom behave the notification is sent
#         -msg <file> contains the message to be sent
#         -emails <space-separated-list> are addresses whom to notify
#         and optional logging parameters that will be used to initialize logging system
#
#   Martin Senger <martin.senger@gmail.com>
#   September 2011
# -----------------------------------------------------------------

use warnings;
use strict;
use Monitor::Simple;
use Log::Log4perl qw(:easy);

# read command-line arguments
my ($service_id, $msgfile, $emails) =
    Monitor::Simple::Utils->parse_notifier_args (\@ARGV);
my $subject = "Notification from the service [$service_id]";
my $list_of_emails = join (',', @$emails);

# input file with the notification message
open (MSG, '<', $msgfile)
    or LOGDIE ("Cannot open file '$msgfile' with the notification message: $!\n");

# output stream to the 'mail' command
my @command = ('mail', '-s', "'$subject'", $list_of_emails);
DEBUG ("[$0] Command: " . join (' ', @command));
open (MAIL, '|-', @command);

# send the message
while (<MSG>) {
    print MAIL $_;
}
close MAIL;
close MSG;

INFO ("[$0] $subject: $list_of_emails");

__END__

