Subversion Repositories camp_sysinfo_client_3

Rev

Rev 201 | Blame | Last modification | View Log | Download | RSS feed

#! /usr/bin/env perl

# script takes a standard sendmail message formatted file (you know the
# one, where you have From:, To:, etc...), pulls the header info out, 
# then uses that to send via the sendEmail script.
# this way, you can use a standard format for an e-mail, but conver it
# to something else.
# of course, that means you are formatting one way, then reformatting
# another, but it is better than rewriting your main script.

use warnings;
use strict;

our $VERSION = '1.1';

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;

my $line;
my $commandLine;

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 ( $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;
   
$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;