#!/bin/bash
# *****************************************************
# file: sendmail.inc.sh
# date: 2005-09-21 22:48
# author: Marko Schulz - <info@tuxnet24.de>
# *****************************************************
#
#
# SYNOPSIS
#
# # load this file in your main script:
# test -f ./send_mail.lib && . ./send_mail.lib || exit 1
#
# # define variables for mail header...
# tname="Marko Schulz" # recipient name
# tmail="info@tuxnet24.de" # recipient mail
# fname="Max Mustermann" # from name
# fmail="mmustermann@example.com" # from mail
# format="multipart/alternative" # mail format: (multipart/alternative|text/plain)
# subject="Testmail..." # subject
# priority="High" # priority: (High|Normal|Low)
#
#
# # define your mail body...
# body=$(
#
# cat <<HERE
# Hallo Marko,
#
# das ist ein
# Text
# der so ausgegeben wird
# wie er
# definiert ist.
#
#
# HERE
#
# )
#
# # sent mail...
# send_mail \
# "$tname" \
# "$tmail" \
# "$fname" \
# "$fmail" \
# "$format" \
# "$subject" \
# "$priority" \
# "$body"
#
#
# *****************************************************
# This function send a mail with the program "sendmail"
# in two formats (HTML/TEXT).
function send_mail() {
local boundary=""
local tname=$1; shift
local tmail=$1; shift
local fname=$1; shift
local fmail=$1; shift
local format=$1; shift
local subject=$1; shift
local priority=$1; shift
local body="$*"
local version="send_mail.lib v1.01"
local mail=""
# get multiple random number or random string...
if which openssl >/dev/null 2>&1; then
boundary=$( openssl rand -base64 24 )
else
boundary=$( echo ${RANDOM}${RANDOM}${RANDOM}${RANDOM} )
fi
# mail header To/From/Subject etc...
mail="${mail}To: \"$tname\" <$tmail>\n"
mail="${mail}Reply-to: \"$fname\" <$fmail>\n"
mail="${mail}From: \"$fname\" <$fmail>\n"
mail="${mail}X-Mailer: Custom-CGI: $version\n"
mail="${mail}MIME-Version: 1.0\n"
mail="${mail}Subject: $subject\n"
mail="${mail}Importance: $priority\n"
# show mail body (HTML)...
if [ "$format" = "multipart/alternative" ]; then
# mail format and decoding...
mail="${mail}Content-Type: $format; boundary=\"------------$boundary\"\n"
mail="${mail}\n"
mail="${mail}This is a multi-part message in MIME format.\n"
# format TEXT...
mail="${mail}--------------$boundary\n"
mail="${mail}Content-Type: text/plain; charset=iso-8859-1\n"
mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
mail="${mail}${body}\n\n"
# replace newline to <br>...
body=$( echo -e "${body}" | \
sed 's/\t/\ \ \ \ /g' | \
sed 's/^ /\ /g' | \
sed 's/$/<br \/>/g' )
# format HTML...
mail="${mail}--------------$boundary\n"
mail="${mail}Content-Type: text/html; charset=iso-8859-1\n"
mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
mail="${mail}<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n\n"
mail="${mail}<html>\n"
mail="${mail}<head>\n"
mail="${mail}<META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">\n"
mail="${mail}\t<title>$subject</title>\n"
mail="${mail}<style type=\"text/css\">\n<!--\n\n"
mail="${mail}.text{ font-family:verdana, helvetica, sans-serif; font-size:10pt; color:black;}\n"
mail="${mail}.copyright{ font-family:verdana, helvetica, sans-serif; font-size:9pt; font-style:italic; color:black;}\n\n"
mail="${mail}//-->\n</style>\n"
mail="${mail}</head>\n\n"
mail="${mail}<body bgcolor=\"#ffffff\" link=\"blue\" alink=\"blue\" vlink=\"blue\">\n"
mail="${mail}<table border=\"0\" cellspacing=\"10\" cellpadding=\"3\" width=\"100%\" height=\"100%\">\n"
mail="${mail}\t<tr>\n"
mail="${mail}\t\t<td width=\"1%\"> </td>\n"
mail="${mail}\t\t<td width=\"5%\" bgcolor=\"#f5f5f5\"> </td>\n"
mail="${mail}\t\t<td width=\"94%\" class=\"text\" valign=\"top\">${body}"
mail="${mail}<hr /><div align=\"right\"><span class=\"copyright\">$version"
mail="${mail} powered by <a href=\"http://www.perlxpress.de\" target=\"_blank\">perlXpress.de</a></span></div></td>\n"
mail="${mail}\t</tr>\n"
mail="${mail}</table>\n"
mail="${mail}</body>\n"
mail="${mail}</html>\n"
mail="${mail}\n--------------$boundary--\n"
mail="${mail}\n"
# end of mail body...
# show mail body (TEXT)...
else
# mail format and decoding...
mail="${mail}Content-Type: $format; charset=iso-8859-1\n"
mail="${mail}Content-Transfer-Encoding: 8bit\n\n"
mail="${mail}${body}\n\n"
mail="${mail}---------------------------------------------------------------------------\n"
mail="${mail}\t\t$version powered by perlXpress.de\n"
mail="${mail}---------------------------------------------------------------------------\n\n\n"
# end of mail body...
fi
# sent mail...
echo -e "${mail}" | /usr/sbin/sendmail -t
}
# *****************************************************
# end of this library...