package Util::Email; ############################################################################## # # DESCRIPTION: Utility to send an email message # # HISTORY: # HISTORY: $Log: Email.pm,v $ # HISTORY: Revision 1.2 2013/04/11 07:12:35 apsop # HISTORY: Strip apostrophes from $subject, so they don't break the # HISTORY: constructed command line. # HISTORY: # HISTORY: Revision 1.1 2012/08/15 08:16:53 apsop # HISTORY: Utility to send an email message, by Bob W. # HISTORY: # HISTORY: # # VERSION: $Revision: 1.2 $ # ############################################################################## use strict; use Carp; # function sendEmail accepts a hash containing # SUB [Subs::Sub, required] # => a blessed reference supporting the Subs::Sub interface # (presumably an instance of a sub-type of Subs::Sub) # TEXT [string, required] # => the body of the email to send # SUBJECT [string, optional] # => the subject of the email to send # defaults to 'message from pipeline' # apostrophes will be removed # TO [string, optional] # => space delimited list of addresses to send the email # defaults to the value of watchers from the procpar file sub sendEmail { my %args = @_; my $sub = $args{SUB}; if (not $sub) { carp("sendEmail: missing SUB"); return; } my $text = $args{TEXT}; if (not $sub) { carp("sendEmail: missing TEXT"); return; } my $jobpar = $sub->jobpar(); my $procpar = $sub->procpar(); my $subject = $args{SUBJECT} || 'message from pipeline'; my $to = $args{TO} || $procpar->read('watchers'); # Remove any apostrophes from $subject, because # they will break the constructed command line. $subject =~ s/\'//g ; my $sequence = $jobpar->read('sequence'); my $tm = time(); my $name = join('_', 'msg', $tm, $sequence); my $dir = '/tmp'; my $path = "$dir/$name"; if (open OUTF, ">$path") { print OUTF $text; close OUTF; my $hostname = `hostname`; chomp $hostname; if ($hostname =~ /^sdc/ ) { my $cmd = "mail -s \'$subject\' $to < $path"; `$cmd`; } else { sendRemote($subject, $to, $dir, $name); } unlink($path); } } sub sendRemote { my ($subject, $to, $dir, $name) = @_; my $path = "$dir/$name"; my $rv = system("scp -q $path apsop\@sdc:$dir >& /dev/null"); if ($rv == 0) { my $cmd1 = "ssh -nq apsop\@sdc \"mail -s \'$subject\' $to < $path\""; my $rval = system($cmd1); system("ssh -nq apsop\@sdc rm -f $path"); } } 1;