| 26 | 
           rodolico | 
           1 | 
           #! /usr/bin/env perl
  | 
        
        
           | 11 | 
           rodolico | 
           2 | 
              | 
        
        
            | 
            | 
           3 | 
           # script takes a standard sendmail message formatted file (you know the
  | 
        
        
            | 
            | 
           4 | 
           # one, where you have From:, To:, etc...), pulls the header info out, 
  | 
        
        
            | 
            | 
           5 | 
           # then uses that to send via the sendEmail script.
  | 
        
        
            | 
            | 
           6 | 
           # this way, you can use a standard format for an e-mail, but conver it
  | 
        
        
            | 
            | 
           7 | 
           # to something else.
  | 
        
        
            | 
            | 
           8 | 
           # of course, that means you are formatting one way, then reformatting
  | 
        
        
            | 
            | 
           9 | 
           # another, but it is better than rewriting your main script.
  | 
        
        
            | 
            | 
           10 | 
              | 
        
        
           | 26 | 
           rodolico | 
           11 | 
           use warnings;
  | 
        
        
            | 
            | 
           12 | 
           use strict;
  | 
        
        
            | 
            | 
           13 | 
              | 
        
        
           | 28 | 
           rodolico | 
           14 | 
           our $VERSION = '1.1';
  | 
        
        
           | 26 | 
           rodolico | 
           15 | 
              | 
        
        
           | 11 | 
           rodolico | 
           16 | 
           my $mailServer;
  | 
        
        
            | 
            | 
           17 | 
           my $smtpUser;
  | 
        
        
            | 
            | 
           18 | 
           my $smtpPass;
  | 
        
        
            | 
            | 
           19 | 
           my $config = '/root/sysinfo_mail';
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           my $emailScript = '/opt/sendEmail/sendEmail.pl';
  | 
        
        
            | 
            | 
           22 | 
           my $logFile = '/tmp/mail.log';
  | 
        
        
            | 
            | 
           23 | 
           my $tls = "'auto'";
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           # just pre-delaring some variables
  | 
        
        
            | 
            | 
           26 | 
           my $from;
  | 
        
        
            | 
            | 
           27 | 
           my $to;
  | 
        
        
            | 
            | 
           28 | 
           my $subject;
  | 
        
        
            | 
            | 
           29 | 
           my $cc;
  | 
        
        
            | 
            | 
           30 | 
           my $bcc;
  | 
        
        
            | 
            | 
           31 | 
           my $message;
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
           | 26 | 
           rodolico | 
           33 | 
           my $line;
  | 
        
        
            | 
            | 
           34 | 
           my $commandLine;
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
           | 11 | 
           rodolico | 
           36 | 
           if ( -e '/root/sysinfo_mail' ) {
  | 
        
        
            | 
            | 
           37 | 
              open DATA,"<$config" or die "Could not read config $config: $!\n";
  | 
        
        
            | 
            | 
           38 | 
              my $commands = join( '', <DATA> );
  | 
        
        
            | 
            | 
           39 | 
              die "Could not parse $config\n" unless eval( $commands );
  | 
        
        
            | 
            | 
           40 | 
           }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
           while ( $line = <> ) {
  | 
        
        
            | 
            | 
           43 | 
              if ( $line =~ m/^From:\s+(.*)$/ ) {   
  | 
        
        
            | 
            | 
           44 | 
                 $from = $1;   
  | 
        
        
            | 
            | 
           45 | 
                 chomp $from;   
  | 
        
        
            | 
            | 
           46 | 
              }
  | 
        
        
            | 
            | 
           47 | 
              if ( $line =~ m/^To:\s+(.*)$/ ) { 
  | 
        
        
            | 
            | 
           48 | 
                 $to = $1;
  | 
        
        
            | 
            | 
           49 | 
                 chomp $to;
  | 
        
        
            | 
            | 
           50 | 
              }
  | 
        
        
            | 
            | 
           51 | 
              if ( $line =~ m/^Subject:\s+(.*)$/ ) {
  | 
        
        
            | 
            | 
           52 | 
                 $subject = $1;
  | 
        
        
            | 
            | 
           53 | 
                 chomp $subject;
  | 
        
        
            | 
            | 
           54 | 
              }
  | 
        
        
            | 
            | 
           55 | 
              if ( $line =~ m/^cc:\s+(.*)$/ ) {
  | 
        
        
            | 
            | 
           56 | 
                 $cc = $1;
  | 
        
        
            | 
            | 
           57 | 
                 chomp $cc;
  | 
        
        
            | 
            | 
           58 | 
              }
  | 
        
        
            | 
            | 
           59 | 
              if ( $line =~ m/^bcc:\s+(.*)$/ ) {
  | 
        
        
            | 
            | 
           60 | 
                 $bcc = $1;
  | 
        
        
            | 
            | 
           61 | 
                 chomp $bcc;
  | 
        
        
            | 
            | 
           62 | 
              }
  | 
        
        
            | 
            | 
           63 | 
              if ( $line =~ m/^$/ ) { # blank line, so the rest is the message
  | 
        
        
            | 
            | 
           64 | 
                 $message = join( '', <> );
  | 
        
        
            | 
            | 
           65 | 
              }
  | 
        
        
            | 
            | 
           66 | 
           }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
           #print "From: $from\nTo: $to\nSubject: $subject\n";
  | 
        
        
            | 
            | 
           69 | 
           #print "cc: $cc\n" if $cc;
  | 
        
        
            | 
            | 
           70 | 
           #print "bcc: $bcc\n" if $bcc;
  | 
        
        
            | 
            | 
           71 | 
           #print "\n$message";
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
           my %CLIParams ;
  | 
        
        
            | 
            | 
           74 | 
           $CLIParams{'-f'}  = qq/$from/;
  | 
        
        
            | 
            | 
           75 | 
           $CLIParams{'-t'}  = qq/$to/;  
  | 
        
        
            | 
            | 
           76 | 
           $CLIParams{'-u'}  = qq/$subject/;
  | 
        
        
            | 
            | 
           77 | 
           $CLIParams{'-s'}  = qq/$mailServer/;
  | 
        
        
            | 
            | 
           78 | 
           $CLIParams{'-xu'} = qq/$smtpUser/;  
  | 
        
        
            | 
            | 
           79 | 
           $CLIParams{'-xp'} = qq/$smtpPass/;  
  | 
        
        
            | 
            | 
           80 | 
           $CLIParams{'-cc'} = qq/$cc/ if $cc; 
  | 
        
        
            | 
            | 
           81 | 
           $CLIParams{'-bcc'}= qq/$bcc/ if $bcc;
  | 
        
        
            | 
            | 
           82 | 
           $CLIParams{'-l'}  = qq/$logFile/;
  | 
        
        
            | 
            | 
           83 | 
           $CLIParams{'-o tls='} = qq/$tls/ if $tls;
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
           $commandLine = qq/$emailScript/;
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           die "Could not find executable $commandLine in sendEmailScript\n" unless -x $commandLine;
  | 
        
        
            | 
            | 
           88 | 
           $commandLine .= ' -q'; # make it act quietly
  | 
        
        
            | 
            | 
           89 | 
           foreach my $key ( keys %CLIParams ) {
  | 
        
        
            | 
            | 
           90 | 
              # depending on whether the key ends in an = or not, we will or will not use a space
  | 
        
        
            | 
            | 
           91 | 
              # between the key and the parameter
  | 
        
        
            | 
            | 
           92 | 
              $commandLine .= $key =~ m/=$/ ? " $key'$CLIParams{$key}'" : " $key '$CLIParams{$key}'";
  | 
        
        
            | 
            | 
           93 | 
           }
  | 
        
        
           | 28 | 
           rodolico | 
           94 | 
           # $commandLine .= ' ' . $$parameters{'otherCLParams'} if $$parameters{'otherCLParams'};
  | 
        
        
           | 11 | 
           rodolico | 
           95 | 
           #print "$commandLine\n";
  | 
        
        
            | 
            | 
           96 | 
           open SENDMAIL, "|$commandLine" or die "Could not open [$commandLine]: $!\n";
  | 
        
        
            | 
            | 
           97 | 
           print SENDMAIL $message;
  | 
        
        
            | 
            | 
           98 | 
           close SENDMAIL;
  |