-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMailer.pm
executable file
·72 lines (65 loc) · 2.04 KB
/
Mailer.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl
use strict 'vars';
use POSIX 'uname';
use IO::File;
#------- retrieve the local host name
my $thisHost= (&POSIX::uname)[1]; # can't trust HOST envvar under condor, because for getenv=TRUE
# it will either inherit HOST and HOSTNAME from submitter env
# or not set anything for getenv=FALSE
# --> so we use POSIX uname() call here
my $thisUser= $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
chomp($thisHost);
$thisHost=lc($thisHost);
my ($thisDomain)=($thisHost=~/^[\w\-]+\.(.+)/);
my $def_domain = $thisDomain;
$def_domain=~s/^dfci\./jimmy\./;
my $def_host = $thisHost;
#----------------------
sub send_mail {
my $hash=shift;
$hash->{'from'}=$thisUser.'@'.$def_host
unless defined($hash->{'from'});
my $to=$hash->{'to'};
unless ($to) {
$hash->{'to'}=$thisUser.'@'.$def_domain;
}
else {
$hash->{'to'}=$to.'@'.$def_domain unless $to =~ m/@/;
}
my $file;
local *ADDFILE;
if (defined($hash->{file})) {
#warning: assumes it's a decent, short text file!
local $/=undef; #read whole file
open(ADDFILE, '<'.$hash->{file}) || return "Cannot open file ".$hash->{file}."\n";
$file=<ADDFILE>;
close ADDFILE;
}
my $body = $hash->{'body'};
$body.=$file;
my $fh;
$fh = IO::File->new('| sendmail -t -oi') ||
die("Mailer.pm error: Cannot open the sendmail pipe\n");
$fh->print("To: $hash->{to}\n");
$fh->print("From: $hash->{from}\n");
$fh->print("Subject: $hash->{subj}\n\n");
$fh->print($body);
$fh->close();
print STDERR "[DEBUG] ======================> mail sent!\n";
#-- create the Mail::Mailer object and send the message:
# my $mailer = Mail::Mailer->new();
# $mailer->open({ 'From' => $hash->{'from'},
# 'To' => $hash->{'to'},
# 'Cc' => $hash->{'cc'},
# 'Subject' => $hash->{'subj'}
# })
# or die "Mailer.pm error: Can't open. $!\n";
#print $mailer $body;
#$mailer->close();
}
sub thisHost {
return $thisHost;
}
sub thisUser {
return $thisUser;
}