1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use MIME::Lite; |
5 |
|
6 |
my %ext2mime; |
7 |
|
8 |
open(MIME,"/etc/mime.types") || die "mime.types: $!"; |
9 |
while(<MIME>) { |
10 |
chomp; |
11 |
next if (/^ *$/ || /^ *#/); |
12 |
my ($type,$exts) = split(/[ \t]+/,$_,2); |
13 |
if ($exts) { |
14 |
foreach my $ext (split(/[ \t]+/,$exts)) { |
15 |
$ext2mime{$ext} = $type; |
16 |
} |
17 |
} |
18 |
|
19 |
} |
20 |
close(MIME); |
21 |
|
22 |
my $curr_dir = shift @ARGV; |
23 |
|
24 |
if (! $curr_dir) { |
25 |
print "\nUsage: $0 [directory]\n\n"; |
26 |
print "diretory should containt directories which are valid e-mail\n"; |
27 |
print "addresses (like first.last\@pliva.com) and attachements inside\n"; |
28 |
print "that directory (directories without \@ will be ignored !!).\n"; |
29 |
print "All files in selected directory will be ignored.\n\n"; |
30 |
exit 1; |
31 |
} |
32 |
|
33 |
opendir(DIR, $curr_dir) || warn "can't opendir $curr_dir: $!"; |
34 |
my @dirs = grep { !/^\./ && -d "$curr_dir/$_" } readdir(DIR); |
35 |
closedir(DIR); |
36 |
|
37 |
print "Using dirs: ",join(", ",@dirs),"\n\n"; |
38 |
|
39 |
my @failed; |
40 |
|
41 |
sub f { |
42 |
my $msg = shift; |
43 |
push @failed, $msg; |
44 |
print STDERR "$msg\n"; |
45 |
} |
46 |
|
47 |
|
48 |
my $max = $#dirs+1; |
49 |
my $nr = 1; |
50 |
|
51 |
foreach my $dir (@dirs) { |
52 |
|
53 |
if ($dir !~ m/\@/) { |
54 |
f("directory '$dir' don't have @ in name!"); |
55 |
next; |
56 |
} |
57 |
|
58 |
if (! opendir(DIR, "$curr_dir/$dir")) { |
59 |
f("can't open '$dir': $!"); |
60 |
next; |
61 |
} |
62 |
|
63 |
my @files = grep { !/^\./ && -f "$curr_dir/$dir/$_" } readdir(DIR); |
64 |
closedir(DIR); |
65 |
|
66 |
my $msg = new MIME::Lite( |
67 |
From =>'Zeljko Covic <zeljko.covic@pliva.hr>', |
68 |
To => $dir, |
69 |
Subject =>'Assessment Schedule', |
70 |
Type =>'multipart/mixed'); |
71 |
|
72 |
$msg->add('Errors-To' => 'Iva.Mladinic@pliva.hr'); |
73 |
|
74 |
# debug |
75 |
$msg->add('X-To' => $dir); |
76 |
|
77 |
print STDERR "[$nr/$max] working on $dir...\n"; |
78 |
$nr++; |
79 |
|
80 |
attach $msg ( |
81 |
Type =>'TEXT', |
82 |
Data =>"Please find attached more information about the assessment with relevant files included."); |
83 |
|
84 |
foreach my $file (@files) { |
85 |
|
86 |
my $type = 'application/octet-stream'; |
87 |
|
88 |
if ($file =~ m/\.([^\.]+)$/) { |
89 |
$type = $ext2mime{$1} if ($ext2mime{$1}); |
90 |
} |
91 |
|
92 |
print STDERR "\tadding '$file' ($type)\n"; |
93 |
|
94 |
attach $msg ( |
95 |
Type => $type, |
96 |
Path => "$curr_dir/$dir/$file", |
97 |
Filename => $file); |
98 |
|
99 |
} |
100 |
|
101 |
if (! $msg->send) { |
102 |
f("can't send e-mail to: $dir"); |
103 |
} |
104 |
} |
105 |
|
106 |
if (@failed) { |
107 |
open(ERRORS,"> errors.log") || warn "can't open errors.log: $!"; |
108 |
print "\nERRORS during this run (also saved in errors.log):\n"; |
109 |
print join("\n",@failed); |
110 |
print ERRORS join("\n",@failed); |
111 |
print "\n"; |
112 |
close(ERRORS); |
113 |
} |