[Kolab-devel] mail from command line - kolab2

Stephan Buys list at codefusion.co.za
Tue Jan 18 10:33:46 CET 2005


Here is a nice perl utility that I discovered a while back. Kolab contains
enough of its dependencies for it to run without a problem (not tested with
Kolab2 yet).

On Tuesday 18 January 2005 11:21, Hamish wrote:
> Stephan Buys wrote:
> > Just use /kolab/sbin/sendmail
> There does not seem to be a way to modify the subject line with a 
> switch... The only way I found to do it is to pipe in a properly 
> constructed file ie:
> TO: foo
> FROM: bar
> SUBJECT: completed
> 
> Body text
> .
This is correct.

> I suppose this might be the only way, are there any mailer functions in 
> any of the kolab files? I am thinking maybe to use php (I see some of 
> the filters for kolab already use this)
PHP has built-in functions, but try amail. It is a real treat to work with and
even does attachments, something that is hard even with "mail".




> Thanks,
> H
> 

-- 
Stephan  Buys
Code Fusion cc.
Tel: +27 11 673 0411
Mobile: +27 83 294 1876
Email: s.buys at codefusion.co.za

E-mail Solutions, Kolab Specialists.
http://www.codefusion.co.za
-------------- next part --------------
#!/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 @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  "\@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.
$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';
}


More information about the devel mailing list