dir2mime.pl (1270B)
1 #!/usr/bin/perl 2 3 use v5.10.1; 4 use strict; 5 use MIME::Lite; 6 use File::MMagic; 7 use Getopt::Std; 8 9 my %opt = (); 10 getopts('a:', \%opt); 11 my $attachment_dirname = $opt{a}; 12 13 my $mm = new File::MMagic; 14 15 my $dirname = $ARGV[0]; 16 my $dir; 17 my @dircontents; 18 opendir($dir, $dirname) || die "cannot open $dirname: $!"; 19 @dircontents = readdir($dir); 20 close($dir); 21 22 my $message = MIME::Lite->new( 23 Type => 'multipart/mixed', 24 ); 25 foreach my $file (@dircontents) { 26 $file =~ '^\.' && (print STDERR "skipping $file" && next); 27 my $filepath = $dirname . '/' . $file; 28 my $mimetype = $mm->checktype_filename($filepath); 29 $message->attach( 30 Type => $mimetype, 31 Path => $filepath, 32 Filename => $file, 33 Encoding => 'base64', 34 Disposition => 'inline', 35 ), 36 } 37 38 if ($attachment_dirname ne '') { 39 opendir($dir, $attachment_dirname) || die "cannot open $dirname: $!"; 40 @dircontents = readdir($dir); 41 close($dir); 42 foreach my $file (@dircontents) { 43 $file =~ '^\.' && (print STDERR "skipping $file" && next); 44 my $filepath = $attachment_dirname . '/' . $file; 45 my $mimetype = $mm->checktype_filename($filepath); 46 $message->attach( 47 Type => $mimetype, 48 Path => $filepath, 49 Filename => $file, 50 Encoding => 'base64', 51 Disposition => 'attachment', 52 ), 53 } 54 } 55 56 print $message->as_string;