shellscripts:email

email.sh

Quelle

This little script sends an email with one or more attachments from the commandline. It uses either Perl's MIME:Base64 or openssl to do the Base64 encoding.

#!/bin/sh
 
# Sends an email with attachements need perl with MIME::Base64
 
#path to sendmail
SENDMAIL='/usr/sbin/sendmail'
 
#Your base64 encoder - must accept STDIN and encode to STDOUT
#BASE64="eval openssl enc -base64"
BASE64="perl -e \"use MIME::Base64; print encode_base64(join('',<STDIN>));\""
 
while getopts "s:t:f:m:" OPTION
do
  case $OPTION in
    s ) SUBJECT=$OPTARG;;
    t ) RECEIVER=$OPTARG;;
    m ) MESSAGE=$OPTARG;;
    f ) FROM=$OPTARG;;
  esac
done
 
if [ -z "$SUBJECT" ] || [ -z "$RECEIVER" ]
then
  echo "Usage: `basename $0` -s <subject> -t <receiver> [-f <sender>] [-m <text>] <attachements>"
  exit
fi
 
shift $(($OPTIND - 1))
 
BOUNDARY='000XMAIL000'
DATE=`date -R`
 
if [ ! -z "$FROM" ]
then
  SENDMAIL="$SENDMAIL -t -f $FROM"
else
  SENDMAIL="$SENDMAIL -t"
fi
 
(
echo -en "MIME-Version: 1.0\n"
echo -en "To: <$RECEIVER>\n"
if [ ! -z "$FROM" ]
then
  echo -en "From: <$FROM>\n"
fi
echo -en "Subject: $SUBJECT\n"
echo -en "Date: $DATE\n"
echo -en "Content-Type: multipart/mixed; boundary=\"$BOUNDARY\"\n\n"
 
echo -en "This is a multi-part message in MIME format.\n"
echo -en "--$BOUNDARY\n"
echo -en "Content-Type: text/plain; charset=iso-8859-1; format=flowed\n"
echo -en "Content-Transfer-Encoding: 8bit\n\n"
echo -en "$MESSAGE\n\n"
echo -en "See attached file(s)\n\n"
 
for ATTACHMENT in $@
do
  FILENAME=`basename $ATTACHMENT`
  MIMETYPE=`(file -i $ATTACHMENT |awk '{print $2}') 2>/dev/null`
  echo -en "--$BOUNDARY\n"
  echo -en "Content-Type: $MIMETYPE; name=\"$FILENAME\"\n"
  echo -en "Content-Transfer-Encoding: base64\n"
  echo -en "Content-Disposition: inline; filename=\"$FILENAME\"\n\n" 
  cat $ATTACHMENT | (eval $BASE64)
done
 
echo -en "--$BOUNDARY--\n"
) | (eval $SENDMAIL)

or just just mutt 1)

$ mutt -s"Subject" -i /path/to/bodytext -a /path/to/attachment user@doma.in