Bash-email-with-attachments: Difference between revisions

From I Will Fear No Evil
Jump to navigation Jump to search
(Created page with "====Mail with attachments==== # Install uuencode and sendmail binaries <pre> function fappend { echo "$2">>$1; } send_email2 () { local EMAIL="${2}" FILE2="/tmp/${RANDOM}...")
 
 
Line 76: Line 76:


Note that $1 is the detail information to append to the message body
Note that $1 is the detail information to append to the message body
In general, bsd-mailx seems the most reliable


send_email() {
send_email() {

Latest revision as of 16:13, 15 October 2021

Mail with attachments

  1. Install uuencode and sendmail binaries
function fappend {
    echo "$2">>$1;
}

send_email2 () {
local EMAIL="${2}"
FILE2="/tmp/${RANDOM}mail_msg.txt"
echo -e "${1}" > ${FILE2}
local FAILED="${3}"
local TOTAL="${4}"

TOEMAIL="${EMAIL}"
if [ -z "${EMAIL}" ];then
  TOEMAIL="chubbard@iwillfearnoevil.com";
fi
FREMAIL="monitor@$(hostname).DOMAIN.COM";

#  These likely will be variables in the script
SUBJECT="Monitored host list for ${FILE} Failed: ${FAILED} ${TOTAL}";
local ADDITIONAL="Attached is the list of all checked hosts"

MSGBODY=" ${ADDITIONAL}"
ATTACHMENT="${FILE2}"
MIMETYPE="text/plain" #if not sure, use http://www.webmaster-toolkit.com/mime-types.shtml

# DON'T CHANGE ANYTHING BELOW UNLESS YOU UNDERSTAND IT
TMP="/tmp/tmpfil_123"$RANDOM;
BOUNDARY=`date +%s|md5sum`
BOUNDARY=${BOUNDARY:0:32}
FILENAME=`basename $ATTACHMENT`

rm -rf $TMP;
cat $ATTACHMENT|uuencode --base64 $FILENAME>$TMP;
sed -i -e '1,1d' -e '$d' $TMP;#removes first & last lines from $TMP
DATA=`cat $TMP`

rm -rf $TMP;
fappend $TMP "From: $FREMAIL";
fappend $TMP "To: $TOEMAIL";
fappend $TMP "Reply-To: $FREMAIL";
fappend $TMP "Subject: $SUBJECT";
fappend $TMP "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\"";
fappend $TMP "";
fappend $TMP "This is a MIME formatted message.  If you see this text it means that your";
fappend $TMP "email software does not support MIME formatted messages.";
fappend $TMP "";
fappend $TMP "--$BOUNDARY";
fappend $TMP "Content-Type: text/plain; charset=ISO-8859-1; format=flowed";
fappend $TMP "Content-Transfer-Encoding: 7bit";
fappend $TMP "Content-Disposition: inline";
fappend $TMP "";
fappend $TMP "$MSGBODY";
fappend $TMP "";
fappend $TMP "";
fappend $TMP "--$BOUNDARY";
fappend $TMP "Content-Type: $MIMETYPE; name=\"$FILENAME\"";
fappend $TMP "Content-Transfer-Encoding: base64";
fappend $TMP "Content-Disposition: attachment; filename=\"$FILENAME\";";
fappend $TMP "";
fappend $TMP "$DATA";
fappend $TMP "";
fappend $TMP "";
fappend $TMP "--$BOUNDARY--";
fappend $TMP "";
fappend $TMP "";
#cat $TMP>out.txt
cat $TMP| /usr/sbin/sendmail -t;
rm $TMP;
}

Send simple inline email


Note that $1 is the detail information to append to the message body
In general, bsd-mailx seems the most reliable

send_email() {
local HOST=$(hostname)
local FROM="From: monitor@${HOST}.DOMAIN.COM"
local FAILED="${3}"
local TOTAL="${4}"
local ADDRESS=`/sbin/ifconfig | awk '/inet addr/ {print $2}' | sed 's/.*://g' | head -1`
local FILE2="/tmp/${RANDOM}mail_msg.txt"
echo -e "${1}" > ${FILE2}
echo -e "${1}"
local SUBJECT="Monitored list for something Failed: ${FAILED} Total: ${TOTAL}"
local TO="${2}"
if [ -z "${TO}" ];then
  # local TO='ops-alert@DOMAIN.COM'
  local TO='chubbard@iwillfearnoevil.com'
fi
mail -a "${FROM}" -s "${SUBJECT}" ${TO} < ${FILE2}
rm -f ${FILE2}
}