Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl -w
- #
- #
- # my_sendmail [-m <mail server>]
- # [-s <sujet>]
- # [-f <from>]
- # [-r <reply-to>]
- # [-h <Hello String>]
- # [-c <cc-dests>]
- # [-C <charset>]
- # [-D]
- # [-t <type mime du contenu>]
- # [-M <type mime des documents joints>]
- # [-b <bcc-dests>] <dests> [<attached files>...]
- #
- #-
- 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:C:h:D', \%opts) or &usage;
- my $to = shift or &usage;
- my $verbose = $opts{v};
- my $subject = $opts{s};
- my $charset = $opts{C} || "ISO-8859-1"; # could be "UTF-8"
- 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
- #+
- # séparateur multipart quasi aléatoire
- #-
- my $boundary = '<------------ ';
- my @chrs = ('0' .. '9', 'A' .. 'Z', 'a' .. 'z');
- foreach (0..16) { $boundary .= $chrs[rand (scalar @chrs)]; }
- $boundary .= ' ------------>';
- #+
- # initialisation dialogue smtp
- #-
- my %options = (
- Timeout => 30,
- );
- $options{Hello} = $opts{h} if $opts{h} ;
- $options{Debug} = 1 if $opts{D} ;
- my $smtp = Net::SMTP->new($smtphost, %options ) 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=$charset\n");
- $smtp->datasend("Content-Transfer-Encoding: base64\n");
- #$smtp->datasend("Content-Transfer-Encoding: 8bit\n");
- $smtp->datasend("\n");
- while (<>) {
- # $smtp->datasend($_);
- $smtp->datasend(encode_base64($_));
- }
- #+
- # quelques lignes blanches pour aérer
- #-
- $smtp->datasend("\n\n\n");
- #+
- # boucle sur les fichiers à attacher
- #-
- foreach $file (@files) {
- my $name = basename($file);
- my $extension = (fileparse($file,qr/\.[^.]*?/))[2];
- my $ct;
- # on tente de déterminer 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 attachés.
- Usage:
- my_sendmail [-m <mail server>]
- [-s <sujet>]
- [-f <from>]
- [-r <reply-to>]
- [-h <Hello String>]
- [-c <cc-dests>]
- [-C <charset>]
- [-M <mime type des fichiers joints>]
- [-t <mime type du corps du messages>]
- [-D] (debug)
- [-v]
- [-b <bcc-dests>] <dests> [<attached files>...] < <fichier contenant le texte du message>
- EOD
- exit 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement