#!/usr/bin/perl -w # # # my_sendmail [-m ] # [-s ] # [-f ] # [-r ] # [-c ] # [-t ] # [-M ] # [-b ] [...] # #- use Net::SMTP; use MIME::Base64; use Getopt::Std; use File::Basename; use Sys::Hostname; my $progname; ( $progname = $0 ) =~ s/^.*\///; %ctypes = ( 'tar' => 'application/x-tar', 'man' => 'application/zip', 'au|snd' => 'audio/basic', 'mpg|mp3' => 'audio/mpeg', 'mp4' => 'audio/mp4', 'aif|aiff|aifc' => 'audio/x-aiff', 'wav' => 'audio/x-wav', 'gif' => 'image/gif', 'jpg|jpeg|jpe' => 'image/jpeg', 'png' => 'image/png', 'tiff|tif' => 'image/tiff', 'pbm' => 'image/x-portable-bitmap', 'pgm' => 'image/x-portable-graymap', 'ppm' => 'image/x-portable-pixmap', 'zip' => 'application/x-zip', 'gz|gzip' => 'application/x-gzip', 'css' => 'text/css', 'csv' => 'text/csv', 'htm|html' => 'text/html', 'txt|g|h|c|cc|hh|m|f90' => 'text/plain', 'rtx' => 'text/richtext', 'rtf' => 'text/rtf', 'tsv' => 'text/tab-separated-value', 'xml' => 'text/xml', 'h264' => 'video/h264', 'dv' => 'video/dv', 'mpeg|mpg|mpe' => 'video/mpeg', 'qt|mov' => 'video/quicktime', 'avi' => 'video/msvideo', 'pdf' => 'application/pdf', ); #+ # parsing des options #- getopts('s:f:m:c:b:vM:r:t:', \%opts) or &usage; my $to = shift or &usage; my $verbose = $opts{v}; my $subject = $opts{s}; my @files = (); while ($_= shift) { push @files,$_; } my $from; $from = $opts{f} || (($ENV{USER} || $ENV{LOGNAME} || "unknown") . "\@" . hostname); my $smtphost = $opts{m} || "localhost"; # SMTP mail host #+ # separateur multipart quasi aleatoire #- my $boundary = '<------------ '; my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z'); foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; } $boundary .= ' ------------>'; #+ # initialisation dialogue smtp #- my $smtp = Net::SMTP->new($smtphost, Timeout => 30) or die "Net::SMTP::new: $!\n"; $smtp->mail($from); foreach (split /,/,$to) { $smtp->to($_); } if (my $cclist = $opts{c}) { foreach (split /,/,$cclist) { $smtp->cc($_); } } if (my $cclist = $opts{b}) { foreach (split /,/,$cclist) { $smtp->bcc($_); } } #+ # en-tetes message #- $smtp->data(); $smtp->datasend("From: $from\n"); $smtp->datasend("To: $to\n"); if ($opts{c}) { $smtp->datasend("Cc: $opts{c}\n"); } if (exists($opts{r})) { $smtp->datasend("Reply-To: $opts{r}\n"); } $smtp->datasend("Subject: $subject\n") if $subject; $smtp->datasend("MIME-Version: 1.0\n"); $smtp->datasend("X-Mailer: $progname (MY)\n"); $smtp->datasend("Content-Type: multipart/mixed; boundary=\"$boundary\"\n"); $smtp->datasend("\n"); $smtp->datasend("This is a multipart MIME-coded message\n"); $smtp->datasend("\n"); $smtp->datasend("\n"); #+ # partie textuelle du message (a partir de stdin) #- my $ctype = $opts{t} ? $opts{t} : "text/plain"; $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: $ctype; charset=us-ascii\n"); $smtp->datasend("Content-Transfer-Encoding: 7bit\n"); $smtp->datasend("\n"); while (<>) { $smtp->datasend($_); } #+ # quelques lignes blanches pour aerer #- $smtp->datasend("\n\n\n"); #+ # boucle sur les fichiers a attacher #- foreach $file (@files) { my $name = basename($file); my $extension = (fileparse($file,qr/\.[^.]*?/))[2]; my $ct; # on tente de determiner le content type if ($opts{M}) { $ct=$opts{M}; } else { $ct = "Application/octet-stream"; foreach $type_extensions (keys %ctypes) { if ($extension =~ /^\.($type_extensions)$/i) { $ct = $ctypes{$type_extensions}; last; } } } $smtp->datasend("--$boundary\n"); $smtp->datasend("Content-Type: $ct; name=\"$name\"\n"); $smtp->datasend("Content-Disposition: attachment; filename=\"$name\"\n"); $smtp->datasend("Content-Transfer-Encoding: base64\n"); $smtp->datasend("\n"); open FILE,$file or die "fichier $file introuvable\n"; while (read(FILE, $buf, 60*57)) { $smtp->datasend( encode_base64($buf)); } print STDERR "$progname: attachement fichier $name type $ct\n" if $verbose; } $smtp->datasend("--$boundary--\n"); $smtp->dataend(); sub usage { print STDERR <<'EOD'; my_sendmail: envoi de mails avec documents attaches. Usage: my_sendmail [-m ] [-s ] [-f ] [-c ] [-M ] [-t ] [-v] [-b ] [...] < EOD exit 1; }