My version of mail doesn't do attachments, so I wrote a simple script to
do the job.  It's attached.  The interface is similar to that for mail.

Andrew





On Wed, 9 Jul 2003, Terry Collins wrote:

> Date: Wed, 09 Jul 2003 10:58:11 +1000
> From: Terry Collins <[EMAIL PROTECTED]>
> To: Michael Lake <[EMAIL PROTECTED]>
> Cc: Slug List <[EMAIL PROTECTED]>
> Subject: Re: [SLUG] command Line mailer that can send an attachment
>
> Michael Lake wrote:
> >
> > Terry Collins wrote:
> >
> > > As per subject, need a command line mailer that can send a file as an
> > > attachment.
> >
> > Its called 'mail' and yes it can send attachments.
> > man mail
>
> Thanks.
> hmm, we must have different versions as there is no mention of
> attachments in the version I have (sans rh5.0) as all I can do is send
> it as the message.
>
> I'm wanting to do something like
>
> for o in `ls -1'
>       do
>               mailer -a $o  [EMAIL PROTECTED]
>       done
>
> and prefeerably with an older mailer
>
>
>

--

No added Sugar.  Not tested on animals.  May contain traces of Nuts.  If
irritation occurs, discontinue use.

-------------------------------------------------------------------
Andrew McNaughton           In Sydney
                            Working on a Product Recommender System
[EMAIL PROTECTED]
Mobile: +61 422 753 792     http://staff.scoop.co.nz/andrew/cv.doc


#!/usr/local/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 @ARGV) {
    if ($arg =~ /^-/) {
        $val = shift @ARGV;
        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  "[EMAIL PROTECTED] = @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.
$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, "| /usr/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';
}

-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to