Ask Donneker
Google
Klarmachen zum Ändern! Piratenpartei

www.donneker.defree-downloadsmime_mails_with_attachments_from_shell
 

free-downloads - mime_mails_with_attachments_from_shell

sending mime mails from bash (shell/command line) script for UNIX and Linux:
mime mails with multiple attachments bash shell script by Stefan Hummert
more about the mime mails with multiple attachments bash shell script creator

this scripts can be used to easy send binary attachments from shell as mime mails




mime mails with multiple attachments shell script
this scripts can be used to send mime compatible mail from shell (bash) with multiple attachments


created by Stefan Hummert
last modified 20.04.2009 by Stefan Hummert

GNU GPL

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; version 3 of the License.

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.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Text version: http://www.gnu.org/licenses/gpl.txt
HTML version: http://www.gnu.org/licenses/gpl.html

free downloads:
download mime mails with multiple attachments bash script

#!/usr/bin/bash
#
#
# sending mail with attachments
# requires: 
#  mailx 
#  perl (with -MMIME::Base64)
#  awk, cat, grep, wc, sed
#  /tmp for temporary files
#
#


#set -x 


# maximum filesize of single attachment in bytes
# 10.000.000 bytes
MAX_FILESIZE=10000000
# maximum total size of mail
# 12.000.000 bytes
MAX_MAILSIZE=12000000

show_usage()
{
    echo "usage $0: "
    echo "$0 -t mail@recipient.org [-s subject] [-b File] File1 File2 ..."
}

# Parameter 1 File
get_filetype()
{
   m_CONTENTTYPE="text/plain"
   is_compressed=$(file "$1" | grep "compress" | wc -l | sed 's/ //g')
   if [[ "$is_compressed" = "1" ]]; then
       m_CONTENTTYPE="application/zip"
   fi
}

# Parameter 1 File
check_filesize()
{
    filesize=$(ls -lart $1 | awk '{ print $5 }')
    if (($filesize > $MAX_FILESIZE)); then
        echo "ERROR: File $1 is too big (size=$filesize, maximum allowed=$MAX_FILESIZE)" >&2
        return 1
    fi
}

# prueft die groesse der Mail insgesamt
check_mailsize()
{
    filesize=$(ls -lart $TMPFILE | awk '{ print $5 }')
    if (($filesize > $MAX_MAILSIZE)); then
        echo "ERROR: eMail is too big (size=$filesize, maximum allowed=$MAX_MAILSIZE)" >&2
        exit 2
    fi
}


TMPFILE=/tmp/mymail.$$
declare -a ATTACHMENT

if [ -e $TMPFILE ]; then
    rm -f $TMPFILE
fi

echo "Parameters: $@ "

cmdpar=""
for param in "$@"
do
    if [ "$cmdpar" = "SUBJECT" ]; then
        echo "writing subject: $param"
        m_SUBJECT="$param"
        cmdpar=""
        continue
    fi
    if [ "$cmdpar" = "TO" ]; then
        echo "recipients: $param"
        m_TO="$param"
        cmdpar=""
        continue
    fi
    if [ "$cmdpar" = "BODY" ]; then
        echo "body: FILE=$param (if FILE = - then stdin is read)"
        cmdpar=""
        m_BODY="$param"
        continue
    fi

    case "$param" in
        "-s") cmdpar="SUBJECT";;
        "-t") cmdpar="TO";;
        "-b") cmdpar="BODY";;
        *)    cmdpar=""
              if [ ! -r "$param" ]; then
                  echo "File $param does not exist or is not readable" 
              else 
                  att_idx=${#ATTACHMENT[*]}
                  ATTACHMENT[$att_idx]="$param"
                  echo "adding file $param as ATTACHMENT NR. $att_idx = ${ATTACHMENT[$att_idx]}"
              fi
              ;;
    esac

done

# Parameter check
if [ -z $m_TO ]; then
    echo "Parameter -t is required." >&2
    show_usage $*
    exit 1
fi

# creating the mail
cat /dev/null >$TMPFILE

#############
# Mail header
{
    #echo "FROM: "
    echo "TO: $m_TO
SUBJECT: $m_SUBJECT
MIME-version: 1.0
Content-type: multipart/mixed; boundary=\"fron_$$_tier\"
  
This is a message with multiple parts in MIME format.
--fron_$$_tier
Content-type: text/plain
  
"
} >>$TMPFILE


#############
# Mail body  
{
    if [[ "$m_BODY" = "-" ]] || [[ -r "$m_BODY" ]]; then
        cat "$m_BODY"
    fi
    echo 
} >>$TMPFILE

#############
# Mail Attachments 
for i in "${ATTACHMENT[@]}"
do
   echo "working on attachment \"${i}\"" 
   check_filesize "$i"
   if [[ "$?" = "1" ]]; then
       continue
   fi
   get_filetype "$i"
   {
       echo "--fron_$$_tier"
       echo "Content-type: $m_CONTENTTYPE
Content-Disposition: attachment; filename=$(basename $i);
Content-transfer-encoding: base64

"
   } >>$TMPFILE
   perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}' "$i" >>$TMPFILE

   echo  >>$TMPFILE

   check_mailsize
done


#############
# Mail footer
{
    echo "--fron_$$_tier--

"

} >>$TMPFILE

   check_mailsize

# perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}'
# sending the eMail
cat $TMPFILE | mailx -t
rm -f $TMPFILE

how to use - README instructions, please read before you use the scripts!:

easy to use mail script written in bash for sending mime mails with multiple attachments from command line
written by Stefan Hummert
Version: 1.00
visit http://donneker.de/free-downloads/mime_mails_with_attachments_from_shell/
see also bash scripting guide http://www.donneker.de/HowTo/A-Short-Documentation-Reference-Manuals/bash/

This software is free licenced under the GPL - for more info see
http://www.gnu.org/licenses/gpl.txt

What is the purpose of this tool?
The purpose of this script is to have an easy shell interface for sending mails with attachments from command line.
Now this makes it easy to send zipfiles or binary files with mail without problems.
You can specify multiple attachments and a mail body witch can be read from file or from standard input (stdin). 
To prevent sending too big files in mails this script has also some settings at the beginning which are easily 
customizable to your own needs.

With which systems are the scripts designed to run?
this script runs everywhere with bash shell and the other needed tools (perl, awk, grep, wc, sed, ...)
The scripts are created for Linux, but also tested with Sun Solaris 5.9/5.10. 
Use them also with other OS but care, you could get unexpected results.
You just need the following linux compatible commands:
- mailx
- perl (with -MMIME::Base64)
- awk, cat, grep, wc, sed

What is to do?
you can just copy and or rename this script and use it how you like it.

If you have comments, modifications or tested under other circumstances/OS I would
appreciate you mail this as info to: mime.mails@my.donster.de




---
Die hier und in den Unterverzeichnissen aufgeführten Angebote, Produkte oder Dienstleistungen werden von mir aufgrund eigener Benutzung und Erfahrung empfohlen, soweit nicht anders vermerkt.
vodafone.de Klarmachen zum Ändern! Piratenpartei



Diese Seite bewerten / Vote this site:

Rating: 1.8/5 (13 votes cast)

Disclaimer/Impressum

  visits:

(c) 2006-2009 by Stefan Hummert

Back2Main