inputMethod=`stat -L -c %F /proc/$$/fd/0`
#!/bin/sh
# Purpose: email the parametrised list of files to a set user along with any content piped XOR redirected in.
# ========================
DT=`date '+%Y-%m-%d'`
TMP_FL="/tmp/$0.$$"
MAIL_FL=""
ATTACHMENT=""
MIMETYPE="text/plain"
BOUNDARY=`date +%s|md5sum`
BOUNDARY=${BOUNDARY:0:32}
#----- Tweak to suit -----
MAILTO="To: arober11@somehostorother.com"
FROM="doNotReply@somehostorother.com"
REPLY_TO="doNotReply@somehostorother.com"
SUBJECT_LINE="Files from `uname -a` on $DT"
MSGBODY="Files from `uname -a` on $DT"
#----- CMD locations -----
sendmail="/usr/sbin/sendmail"
sendmailParms=""
readlinkCMD="/usr/bin/readlink"
statCMD="/usr/bin/stat"
basenameCMD="/usr/bin/basename"
#----- Tweak to suit ends-----
#----- Test if anything has been piped or directed at us -----
inputMethod=`$statCMD -L -c %F /proc/$$/fd/0`
# ========================
# Attach file to msg
# ========================
attach_file() {
cat >> $TMP_FL << EOF
--$BOUNDARY
Content-Type: $MIMETYPE; name="$MAIL_FL"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="$MAIL_FL"
`cat $ATTACHMENT`
EOF
}
# ========================
# Create tmp msg file
# ========================
create_msg() {
cat > $TMP_FL <<EOF
From: $FROM
`echo -e $MAILTO`
Reply-To: $REPLY_TO
Subject: $SUBJECT_LINE
Content-Type: multipart/mixed; boundary="$BOUNDARY"
This is a MIME formatted message. If you see this text it means that your
email software does not support MIME formatted messages, but as plain text
encoded you should be ok, with a plain text file.
--$BOUNDARY
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit
Content-Disposition: inline
$MSGBODY
EOF
while [ $# -gt 0 ]
do
if [ -r $1 -a -f $1 -a -s $1 ]
then
MAIL_FL="`$basenameCMD $1`"
ATTACHMENT="$1"
attach_file
fi
shift
done
#See if anything has been piped in via STDIN, if it has add it as another attachment
if [ "$inputMethod" = "fifo" ]
then
cat > "${TMP_FL}.from_pipe.stdin"
MAIL_FL="STDIN_from_PIPE.txt"
ATTACHMENT="${TMP_FL}.from_pipe.stdin"
attach_file
fi
if [ "$inputMethod" = "regular file" ]
then
cat > "${TMP_FL}.from_fl.stdin"
flName="`$readlinkCMD \`$statCMD -L -c %n /proc/$$/fd/0\``"
MAIL_FL="`$basenameCMD $flName`"
ATTACHMENT="${TMP_FL}.from_fl.stdin"
attach_file
fi
echo -e "\n--$BOUNDARY--\n" >> $TMP_FL
}
# ====
# Main
# ====
if [ $# -gt 0 -a -f "$1" -a -r "$1" ]
then
create_msg $@
elif echo $inputMethod | egrep -q "character special"
then
thisCMD=`$basenameCMD $0`
echo
echo "Error: No files passed as arguments, piped in, or re-directed in :("
echo
echo "usage: [|]$thisCMD [<file1.txt> [<file2.txt>*]] [<]"
echo "Purpose: email the paramertised list of files, to a set user, along with any content piped XOR redirected in"
echo "examples: cat file1.txt | $thisCMD"
echo " $thisCMD file1.txt file2.txt"
echo " $thisCMD < file1.txt"
echo " cat file1.txt | $thisCMD file2.txt"
echo " $thisCMD file1.txt file2.txt < file3.txt"
exit 1
else
create_msg
fi
# =================
# Email the file(s)
# =================
$sendmail $sendmailParms -t <$TMP_FL
rm -f $TMP_FL*
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Author
Commented: