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