Subversion Repositories sysadmin_scripts

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
59 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
5
 
65 rodolico 6
use version; our $VERSION = version->declare("v1.0.0");
59 rodolico 7
 
8
 
9
my $mailServer;
10
my $smtpUser;
11
my $smtpPass;
12
my $config = '/root/sysinfo_mail';
13
 
14
my $emailScript = '/opt/sendEmail/sendEmail.pl';
15
my $logFile = '/tmp/mail.log';
16
my $tls = "'auto'";
17
 
18
# just pre-delaring some variables
19
my $from;
20
my $to;
21
my $subject;
22
my $cc;
23
my $bcc;
24
my $message;
25
 
26
if ( -e '/root/sysinfo_mail' ) {
27
   open DATA,"<$config" or die "Could not read config $config: $!\n";
28
   my $commands = join( '', <DATA> );
29
   die "Could not parse $config\n" unless eval( $commands );
30
}
31
 
32
while ( my $line = <> ) {
33
   if ( $line =~ m/^From:\s+(.*)$/ ) {
34
      $from = $1;
35
      chomp $from;
36
   }
37
   if ( $line =~ m/^To:\s+(.*)$/ ) {
38
      $to = $1;
39
      chomp $to;
40
   }
41
   if ( $line =~ m/^Subject:\s+(.*)$/ ) {
42
      $subject = $1;
43
      chomp $subject;
44
   }
45
   if ( $line =~ m/^cc:\s+(.*)$/ ) {
46
      $cc = $1;
47
      chomp $cc;
48
   }
49
   if ( $line =~ m/^bcc:\s+(.*)$/ ) {
50
      $bcc = $1;
51
      chomp $bcc;
52
   }
53
   if ( $line =~ m/^$/ ) { # blank line, so the rest is the message
54
      $message = join( '', <> );
55
   }
56
}
57
 
58
#print "From: $from\nTo: $to\nSubject: $subject\n";
59
#print "cc: $cc\n" if $cc;
60
#print "bcc: $bcc\n" if $bcc;
61
#print "\n$message";
62
 
63
my %CLIParams ;
64
$CLIParams{'-f'}  = qq/$from/;
65
$CLIParams{'-t'}  = qq/$to/;
66
$CLIParams{'-u'}  = qq/$subject/;
67
$CLIParams{'-s'}  = qq/$mailServer/;
68
$CLIParams{'-xu'} = qq/$smtpUser/;
69
$CLIParams{'-xp'} = qq/$smtpPass/;
70
$CLIParams{'-cc'} = qq/$cc/ if $cc;
71
$CLIParams{'-bcc'}= qq/$bcc/ if $bcc;   
72
$CLIParams{'-l'}  = qq/$logFile/;
73
$CLIParams{'-o tls='} = qq/$tls/ if $tls;
74
 
75
my $commandLine = qq/$emailScript/;
76
 
77
die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
78
$commandLine .= ' -q'; # make it act quietly
79
foreach my $key ( keys %CLIParams ) {
80
   # depending on whether the key ends in an = or not, we will or will not use a space
81
   # between the key and the parameter
82
   $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
83
}
84
$commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
85
#print "$commandLine\n";
86
open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
87
print SENDMAIL $message;
88
close SENDMAIL;