Amail.pl

From Kolab Wiki

Jump to: navigation, search

This is a very useful perl script for sending files as attachments in email, good for sending log files etc!

Be careful not to copy and paste the text below. Use the edit version. Otherwise you might have some bugs.

The character '' becomes invisible in the code below.

#!/kolab/bin/perl
##!/codefusion/bin/perl
#
# amail - a command line tool for sending files as mail attachments
#
#  fairly similar in style to 'mail'
#
# usage:
#
#   amail [-s Subject] [-c Cc] [-b      Bcc] [-r Reply-to]
#                 [-f From] [[-a attachment_file] ,...] address, ...
#
#
#  addresses may optionally be preceded by -t, in which
#  case they may appear anywhere in the command line
#
#
#
# Copyright (C) 2000, Andrew McNaughton.
# Distributed under the terms of the Perl Artistic License
#
use MIME::Entity;
unless (@ARGV) {
print <<"END_USAGE";
Usage:
  amail [-s Subject] [-c Cc] [-b Bcc] [-r Reply-to] [-f From] [[-a attachment_file] ,...] address, ...
END_USAGE
exit
}
my %types = qw(
       .ai                             application/postscript
       .aifc                   audio/x-aiff
       .aiff                   audio/x-aiff
       .au                             audio/basic
       .bin                    application/octet-stream
       .c                              text/plain
       .c++                    text/plain
       .cc                             text/plain
       .cdf                    application/x-netcdf
       .csh                    application/x-csh
       .dump                   application/octet-stream
       .dvi                    application/x-dvi
       .eps                    application/postscript
       .exe                    application/octet-stream
       .gif                    image/gif
       .gtar                   application/x-gtar
       .gz                             application/gzip
       .gzip                   application/gzip
       .h                              text/plain
       .hdf                    application/x-hdf
       .hqx                    application/mac-binhex40
       .html                   text/html
       .jar                    application/java-archive
       .jfif                   image/jpeg
       .jpe                    image/jpeg
       .jpeg                   image/jpeg
       .jpg                    image/jpeg
       .mime                   message/rfc822
       .mpeg                   video/mpeg
       .mpg                    video/mpeg
       .nc                             application/x-netcdf
       .pdf                    application/pdf
       .php                    text/html
       .pjp                    image/jpeg
       .pjpeg                  image/jpeg
       .pl                             text/x-perl
       .png                    image/png
       .ps                             application/postscript
       .rgb                    image/x-rgb
       .rtf                    application/x-rtf
       .saveme                 application/octet-stream
       .sh                             application/x-sh
       .shar                   application/x-shar
       .sit                    application/x-stuffit
       .snd                    audio/basic
       .src                    application/x-wais-source
       .tar                    application/x-tar
       .tcl                    application/x-tcl
       .tex                    text/plain
       .text                   text/plain
       .tif                    image/tiff
       .tiff                   image/tiff
       .txt                    text/plain
       .uu                             application/octet-stream
       .wsrc                   application/x-wais-source
       .xwd                    image/x-xwd
       .zip                    application/x-zip-compressed
);
my %encodings = (
       'text'                  =>      'quoted-printable',
   'image'                     =>      'base64',
   'multipart'         =>      '7bit',
   'video'                     =>      'base64',
   'message'           =>      'base64',
   'audio'                     =>      'base64',
       'application'   =>      'base64'
);
while ($arg = shift) {
   if ($arg =~ /^-/) {
       $val = shift;
       print STDERR "$arg => $val\n";
       if    ($arg eq '-s') {  $headers{'Subject'      } = $val;       }
       elsif ($arg eq '-t') {  $headers{'To'           } = $val;       }
       elsif ($arg eq '-c') {  $headers{'Cc'           } = $val;       }
       elsif ($arg eq '-b') {  $headers{'Bcc'          } = $val;       }
       elsif ($arg eq '-r') {  $headers{'Reply-to'     } = $val;       }
       elsif ($arg eq '-f') {  $headers{'From'         } = $val;       }
       elsif ($arg eq '-a') {
               push @attachments, $val;
       }
       else {
               die "Unrecognised switch ($arg)";
       }
   }
       else {
       push @addrs, $arg, @ARGV;
       print STDERR  "\@addrs = @addrs";
       last;
   }
}
unless (@addrs) {
       die "No addresses provided";
}
$headers{'To'} = join ",\n\t", @addrs;
$headers{'Type'} = 'multipart/mixed';
# use Data::Dumper;
# print Dumper \%headers;
$top = build MIME::Entity %headers;
print "debug 1\n";
# put in the contents of stdin.
# [edited by Dma147]
# Next line produces a syntax error, so I've corrected it
# $top->attach  (Data=>join (,<>));
$top->attach  (Data=>join (',<>'));
print "debug 2\n";
# attachments specified on the command line
foreach $filename (@attachments) {
       unless ((-f $filename) && (-r $filename)){ die "Can't read $filename\n" };
   $mime_type = &mime_type($filename);
print  "\$mime_type =$mime_type\n";
   $encoding = &encoding($mime_type);
print "\$encoding=$encoding\\n";
   attach $top Path    => $filename,
               Type    => $mime_type,
               Encoding=> $encoding;
}
#$top->print(\*STDOUT);
# Send it:
open MAIL, "| /kolab/sbin/sendmail -t -i" or die "open: $!";
$top->print(\*MAIL);
close MAIL;
sub mime_type {
       my($filename) = @_;
   ($suffix) = $filename =~ /(\.[^\.]+)$/;
   ($suffix) = lc $suffix;
   $types{$suffix} || 'application/octet-stream';
}
sub encoding {
       my($mime_type) = @_;
   my ($major,$minor) = split /\//, $mime_type, 2;
   $encodings{$major} || 'base64';
}
Personal tools