Mailit

Aus SiWiki
Wechseln zu: Navigation, Suche

Website of mailit.

Download from ZweierGit: https://git.zweiernet.ch/sigi/Mailit.

Or get it with: "wget --no-check-certificate https://git.zweiernet.ch/sigi/Mailit/raw/master/mailit"


Changes:

- 06.01.2016: sigi: added the -m -b parameters.


#!/usr/bin/perl
#
# mailit
#
# Copyright (c) 1998, 2016 by Peter_Siegrist(SystemLoesungen)  (PSS@ZweierNet.ch)
# 
# All Rights reserved.
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License as 
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details.    
#
 
#----------------------------------------------
#-- INITIAL SETTINGS TO BE MODIFIED BY USERS
#----------------------------------------------
 
BEGIN { 
#-- Set Path to MIME::Lite Module if not in @INC
#-- i.e. $MIME_LITE_PATH = "/home/myhome/lib/";
#-- uncomment both of the next two lines therefor 
 
#    $MIME_LITE_PATH = "$ENV{HOME}/lib/";    # <=== here we are
#    unshift( @INC, "$MIME_LITE_PATH" ) 
}
 
#-- Set Host / Domain Part of email Address: name@$DOMAIN_NAME
#-- i.e. $DOMAIN_NAME   = "myhost.ch";
$DOMAIN_NAME   = "ZweierNet.ch";                  # <=== here we are
 
#-- END INITIAL SETTINGS
 
 
#----------------------------------------------
#-- Modules
#----------------------------------------------
 
use MIME::Lite;
use Getopt::Std;
 
 
#----------------------------------------------
#-- VAR's
#----------------------------------------------
 
 
$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'
);
 
 
 
#----------------------------------------------
#-- SUB's
#----------------------------------------------
 
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";
}
 
 
#----------------------------------------------
#-- MAIN
#----------------------------------------------
 
#----------------------------------------------
#-- get options:
#----------------------------------------------
 
#       -f      <from>
#       -t      <to>
#       -c      [<cc>]          comma separated
#       -s      <subject>
#       -a      <attachment(s)> comma separated
#       -m      <mime-type:filename>     eg. application/pdf:attachmentpdf
#       -b      <body text if -m is used>
#       -p      read from pipe if set
#       -h      help
#       -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 ( <DATA> ) { 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;
}
 
 
 
#----------------------------------------------
#-- read body from stdin  ( if option p is set )
#----------------------------------------------
 
if ( $p == 1 ) {
    while ( <STDIN> ) {
        last if /^\.$/;     # Finish input with single dot line
        $body .= $_;
    }
}
 
 
#----------------------------------------------
#-- create mime object
#----------------------------------------------
 
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 );
}
 
 
 
#----------------------------------------------
#-- parse filetyp / attach files
#----------------------------------------------
 
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;
    }	
}
 
 
 
 
#----------------------------------------------
#-- send message
#----------------------------------------------
 
$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' part as well as the source setting
    $DOMAIN_NAME as '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)