Mailit

Aus Si:Wiki von Siegrist SystemLösungen - Informatik und Rezepte
Wechseln zu: Navigation, Suche
  1. !/usr/bin/perl
  2. mailit
  3. Copyright (c) 1998, 2016 by Peter_Siegrist(SystemLoesungen) (PSS@ZweierNet.ch)
  4. All Rights reserved.
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  1. ----------------------------------------------
  2. -- INITIAL SETTINGS TO BE MODIFIED BY USERS
  3. ----------------------------------------------

BEGIN {

  1. -- Set Path to MIME::Lite Module if not in @INC
  2. -- i.e. $MIME_LITE_PATH = "/home/myhome/lib/";
  3. -- uncomment both of the next two lines therefor
  1. $MIME_LITE_PATH = "$ENV{HOME}/lib/"; # <=== here we are
  2. unshift( @INC, "$MIME_LITE_PATH" )

}

  1. -- Set Host / Domain Part of email Address: name@$DOMAIN_NAME
  2. -- i.e. $DOMAIN_NAME = "myhost.ch";

$DOMAIN_NAME = "ZweierNet.ch"; # <=== here we are

  1. -- END INITIAL SETTINGS


  1. ----------------------------------------------
  2. -- Modules
  3. ----------------------------------------------

use MIME::Lite; use Getopt::Std;


  1. ----------------------------------------------
  2. -- VAR's
  3. ----------------------------------------------


$body = "";

my %mimes = ( '.doc' => 'application/msword', '.rtf' => 'application/rtf',

   			'.xls'  => 'application/excel',
   			'.xlt'  => 'application/excel',
   			'.pps'  => 'application/pps',
   			'.ppt'  => 'application/ppt',
   			'.pot'  => 'application/pot',
   			'.fmx'  => 'application/x-framemaker',
   			'.ai '  => 'application/postscript',
   			'.eps'  => 'application/postscript',
   			'.ps '  => 'application/postscript',
   			'.sxw'  => 'application/msword',
   			'.odt'	=> 'application/vnd.oasis.opendocument.text',
   			'.ods'	=> 'application/vnd.oasis.opendocument.spreadsheet',
   			'.odp'	=> 'application/vnd.oasis.opendocument.presentation',
   			'.odg'	=> 'application/vnd.oasis.opendocument.graphics',
   			'.odc'	=> 'application/vnd.oasis.opendocument.chart',
   			'.odb'	=> 'application/vnd.oasis.opendocument.database',
   			'.odm'	=> 'application/vnd.oasis.opendocument.text-master',
   			'.odf'	=> 'application/vnd.oasis.opendocument.formula',
   			'.html' => 'text/html',
   			'.htm'  => 'text/html',

'.xml' => 'text/xml', '.xsl' => 'text/xml',

   			'.jpg'  => 'image/jpeg',
   			'.jpeg' => 'image/jpeg',
   			'.jpe'  => 'image/jpeg',
   			'.png'  => 'image/png',
   			'.gif'  => 'image/gif',
   			'.bmp'  => 'image/bmp',
   			'.tiff' => 'image/tif',
   			'.tif'  => 'image/tif',
   			'.xbm'  => 'image/x-xbitmap',
   			'.zip'  => 'application/zip',
   			'.Z'  => 'application/x-tar',
   			'.gz'  => 'application/x-tar',
   			'.tar'  => 'application/x-tar',

'.pcap' => 'application/pcap',

   			'.hqx'  => 'application/mac-binhex40',
   			'.pdf'  => 'application/pdf'

);


  1. ----------------------------------------------
  2. -- SUB's
  3. ----------------------------------------------

sub usage {

   print "usage: $0 \n\t-f [<from>] \n\t-t <to>\t\t\tList possible\n\t-c [<cc>]\t\tList possible\n\t-s [<subject>] \n\t-m [<mime-type:filename>] \teg. application/pdf:doc.pdf\n\t-a [<attachment>]\tComma separated list of files to attach (no spaces)\n\t-p\t\t\tread body data from STDIN if set\n\t-h help\n\t-H Manual Page\n";

}


  1. ----------------------------------------------
  2. -- MAIN
  3. ----------------------------------------------
  1. ----------------------------------------------
  2. -- get options:
  3. ----------------------------------------------
  1. -f <from>
  2. -t <to>
  3. -c [<cc>] comma separated
  4. -s <subject>
  5. -a <attachment(s)> comma separated
  6. -m <mime-type:filename> eg. application/pdf:attachmentpdf
  7. -b <body text if -m is used>
  8. -p read from pipe if set
  9. -h help
  10. -H manpage

getopt('tscafmb');

my $p = 0; my $from = "$ENV{USER}@$DOMAIN_NAME"; my $subject = "No Subject"; my $to = undef; my $cc = ""; my $ifile = ""; my $imime = ""; my @attachment = ();

$opt_h && do { &usage; exit; }; $opt_H && do { while ( ) { print; } exit; }; $opt_f && do { $from = $opt_f; }; $opt_s && do { $subject = $opt_s; }; $opt_c && do { $cc = $opt_c; }; $opt_m && do { ($imime, $ifile) = split ':', $opt_m;

               };

$opt_b && do { $itext = $opt_b;

               };

$opt_a && do { @attachment = split ',', $opt_a; }; $opt_p && do { $p = 1; }; if ( $opt_t ) { $to = $opt_t; } else { print "missing mandatory argument: -t\n";

   	&usage;
   	exit;

}


  1. ----------------------------------------------
  2. -- read body from stdin ( if option p is set )
  3. ----------------------------------------------

if ( $p == 1 ) {

   while ( <STDIN> ) {
       last if /^\.$/;     # Finish input with single dot line
       $body .= $_;
   }

}


  1. ----------------------------------------------
  2. -- create mime object
  3. ----------------------------------------------

chomp($to); chomp($from);

$m = MIME::Lite->new(

               From    => $from,
               To      => $to,
               Subject => $subject,
               Type    => 'multipart/mixed');

if ( $opt_m ) {

   attach $m   Type        => 'TEXT',
               Encoding    => 'quoted-printable',
               Data        => "$itext";
   attach $m
           	Type        => $imime,
           	Encoding    => 'quoted-printable',
           	Disposition => 'attachment',
           	Data        => $body,
           	Filename    => "$ifile";

} else { attach $m Type => 'TEXT', Encoding => 'quoted-printable', Data => $body; }

if ( $cc ) {

   $m->add( Cc => $cc );

}


  1. ----------------------------------------------
  2. -- parse filetyp / attach files
  3. ----------------------------------------------

while ( defined( $att = shift @attachment ) ) { my ($ftyp) = ($att =~ /.+(\.\w{1,4})$/i); $ftyp = lc $ftyp; if ( exists $mimes{$ftyp} ) { if ( $mimes{$ftyp} =~ /text\// ) { attach $m

       		Type        => $mimes{$ftyp},
       		Encoding    => 'quoted-printable',
       		Disposition => 'attachment',
       		Path        => "$att";
   		next;
   	} else {
   		attach $m
       		Type        => $mimes{$ftyp},
       		Disposition => 'attachment',
       		Path        => "$att";
   		next;

} } else { attach $m

       	Type        => 'text/plain',
       	Encoding    => 'base64',
       	Disposition => 'attachment',
       	Path        => "$att";
       next;
   }	

}



  1. ----------------------------------------------
  2. -- send message
  3. ----------------------------------------------

$m->send;



__END__

NAME

   mailit - send mail to users, including attachments

SYNOPSIS

   mailit  [ -hH ]  
           -t recipient,...  [ -f sender ]  [ -c recipient,... ] 
           [ -a attachment,... ] [ -m mime-type:filename ] [ -b body text if -m ] [ -s subject ] [ -p ]

DESCRIPTION

   mailit is a front end to send mails including attachments from 
   the command line. It uses sendmail to deliver the message to the
   corret place. mailit is based on MIME::Lite from ZeeGee Software
   Inc.

   With -p flag set it reads from standard input up to an EOF or a
   single dot line. That means it can read some data through a Unix
   Pipe till EOF or, if you prefere read in interactively, terminate
   input with a single dot in the line. In interactive mode mailit
   will prompt you for the body.
   The data you entered this way will be sent as the body of the mail
   setting text/plain as Content-Type.
   mailit determines the 'sender address' from both the environment 
   variable HOME for the 'user' as well as the source setting
   $DOMAIN_NAME for the 'domain'.


OPTIONS

   -t recipient,...
       email address(es) of the recipient(s). If more than one separate
       by commas. (the To: Field)
   -f sender
       sender of the email if you not want to determine it by mailit.
       (The From: Field)
   -c recipient
       one or more CC: addresses. (The CC: Field)
   -a attachment
       one or more comma separated file names to be attached to the
       mail. see below for further discussion.
   -m mime-type:filename
       the mime-type and the filename of the document. This is mainly used for
       situations where you pipe (-p) a document into mailit without any
       information about the mime-type. 
       Mail recipients get the piped data as attachment of type 'mime-type'
       with name 'filename'. Use the -b parameter to give the body a text.
   
   -b body text for -m
       the body text if piping some data with -m and -p to the program .
       
   -s subject
       the subject of the mail. Quote it if you use Special Characters.
       (The Subject: Field)
   -p  mailit reads the body from standard input up to an EOF or, in
       interactive mode up to a single dot line. Without the -p flag
       mailit does not read nor send any body.
   -h  help.
   -H  man page like help.
       
       

ATTACHMENTS

   mailit knows some type of files so it can set the correct
   Content-Type as well as the correct Encoding of the attachment.
   
   Text Media Types are encoded as 'quoted-printable'  whereas other
   known Media Types are encoded 'base64'.
   You can expand the list of known Media Types by adding a 
   'type => encoding' entry in the %mimes hash.
   
   All unknown Media Types will be sent as 'text/plain' with 'binary'-Encoding.
       
       

INSTALLATION

   mailit uses the MIME::Lite Modul for creating Mail Headers. 
   MIME::Lite for his part uses 'sendmail' to send the message.
   Therefore you have to install 'sendmail' as well as the
   MIME::Lite Modul from ZeeGee Software Inc. (www.zeegee.com).
   If this modul can not be found in the @INC Array, that means you
   do not have a standard installation of this module therefore you have to set
   the path of the module in the INITIAL USER SETTINGS Part of the 
   source script ($MIME_LITE_PATH). (see there).
   In some cases mailit can not determine the correct Senders domain
   name. If so, set the Senders domain name in the INITIAL USER 
   SETTINGS Part of the source script ($DOMAIN_NAME). (see there).


ENVIRONMENT VARIABLES

   mailit takes the senders name (that is, the user part of the email
   address (user@...) ) from the Environment Variable USER. Make sure
   this variable is set correctly.


EXAMPLES

   To send just two Attachments. According to standard the Subject
   in this case is set to 'No Subject'.
       mailit -t shorty@host.ch -a /doc/file1.html,/doc/file2.tar.Z


   Full use:
       cat body.txt | mailit -t shorty@host.ch,secnd@addr.ch 
           -f froma@host2.ch -a /doc/file1.html,/doc/file2.tar.Z 
           -c cc1@addr.ch,cc2@addr.ch -p -s "This my Files"
   -m and -b use:
       somefile2pdf somefile.xxx | mailit -t shorty@host.ch -m "application/pdf:somefile.pdf" -b "Attached file for you" -p
       

NOTES

   mailit is a Perl Program. You will probably need Perl 5.005 or 
   better as well as Mime-Lite 2.117 or better to run.
   
   Suggestions are welcome.


AUTHOR

   Peter Siegrist http://PSS.ZweierNet.ch (pss@ZweierNet.ch)