Subversion Repositories sysadmin_scripts

Rev

Rev 65 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

use strict;
use warnings;



my $mailServer;
my $smtpUser;
my $smtpPass;
my $config = '/root/sysinfo_mail';

my $emailScript = '/opt/sendEmail/sendEmail.pl';
my $logFile = '/tmp/mail.log';
my $tls = "'auto'";

# just pre-delaring some variables
my $from;
my $to;
my $subject;
my $cc;
my $bcc;
my $message;

if ( -e '/root/sysinfo_mail' ) {
   open DATA,"<$config" or die "Could not read config $config: $!\n";
   my $commands = join( '', <DATA> );
   die "Could not parse $config\n" unless eval( $commands );
}

while ( my $line = <> ) {
   if ( $line =~ m/^From:\s+(.*)$/ ) {
      $from = $1;
      chomp $from;
   }
   if ( $line =~ m/^To:\s+(.*)$/ ) {
      $to = $1;
      chomp $to;
   }
   if ( $line =~ m/^Subject:\s+(.*)$/ ) {
      $subject = $1;
      chomp $subject;
   }
   if ( $line =~ m/^cc:\s+(.*)$/ ) {
      $cc = $1;
      chomp $cc;
   }
   if ( $line =~ m/^bcc:\s+(.*)$/ ) {
      $bcc = $1;
      chomp $bcc;
   }
   if ( $line =~ m/^$/ ) { # blank line, so the rest is the message
      $message = join( '', <> );
   }
}

#print "From: $from\nTo: $to\nSubject: $subject\n";
#print "cc: $cc\n" if $cc;
#print "bcc: $bcc\n" if $bcc;
#print "\n$message";

my %CLIParams ;
$CLIParams{'-f'}  = qq/$from/;
$CLIParams{'-t'}  = qq/$to/;
$CLIParams{'-u'}  = qq/$subject/;
$CLIParams{'-s'}  = qq/$mailServer/;
$CLIParams{'-xu'} = qq/$smtpUser/;
$CLIParams{'-xp'} = qq/$smtpPass/;
$CLIParams{'-cc'} = qq/$cc/ if $cc;
$CLIParams{'-bcc'}= qq/$bcc/ if $bcc;   
$CLIParams{'-l'}  = qq/$logFile/;
$CLIParams{'-o tls='} = qq/$tls/ if $tls;
   
my $commandLine = qq/$emailScript/;

die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
$commandLine .= ' -q'; # make it act quietly
foreach my $key ( keys %CLIParams ) {
   # depending on whether the key ends in an = or not, we will or will not use a space
   # between the key and the parameter
   $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
}
$commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
#print "$commandLine\n";
open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
print SENDMAIL $message;
close SENDMAIL;