Dears,
I've written a script which allows me to send mails in different formats with different attaches. Now I still want to add a feature to this script. My users would like to be able to receive a "read" or "delivered" receipt for their mails.
The script send mails on behalve of an specific exchange user and is triggered from a web page or another script or a DB. The user (not the unix account) on which behalf the mail will be sent needs confirmation about it. Which codes do I need to use to implement this request ?
P.S.: any enhancements are also welcom. For example the trick I do with mpack, isn't there a more easy way to convert a binary ?
Something in the way of input stream | converter | output stream ?
Underneath you find the script.
#!/usr/bin/ksh
########################################
# Script to send mails in HTML or TEXT, with or whitout attach
# Author : LP
# Syntax : mailto.sh <TYPE> <SUBJECT> <BODY> <TO_ADDRES> [FROM_ADDRES] [<ATTACHEMENT> <ATTACH_TYPE>]
########################################
### vars
##################
export min_para=4
export log=`date +%d%m%Y%H%M%S`.$#.log
first_attach=0
### functions
##################
function Error
{
mailx -s "ERROR TO SEND MAIL " $FROM_ADDRES < $log;
rm $log;
exit 1;
}
function lower
{
echo $1 | tr [:upper:] [:lower:];
}
function upper
{
echo $1 | tr [:lower:] [:upper:];
}
function Syntax
{
echo "Syntax = mailto.sh <TYPE> <SUBJECT> <BODY> <TO_ADDRES> [FROM_ADDRES] [<ATTACHEMENT> <ATTACH_TYPE>]";
echo "Where TYPE = \"t\" for text only mails";
echo " \"h\" for html only mails";
echo " \"ta\" for text with an attachement";
echo " \"ha\" for html with an attachement";
echo "You MUST provide a TYPE, this is Mandatory";
echo "Where SUBJECT = the subject of the mail (Mandatory)";
echo "Where BODY = the name of the file which contains the body for the mail (Mandatory)";
echo "Where TO_ADDRES = the mailadress of the recipients (Mandatory)";
echo " you can provide as many adresses as you like.";
echo" in this case you need to enclose the adresses with double quotes";
echo" and seperate them with a space.";
echo " example: \"adres1@exchage.bc adres@aserver.com adres3@belgacom.be\"";
echo "Where FROM_ADDRES = the mailadress of the sender (Optional)";
echo " If not provided, the adres of the unix account will be used.";
echo "Where ATTACHEMENT = the attachement that has to be sent with the mail (Optional)";
echo "Where ATTACH_TYPE = the type of attachement that has to be sent with the mail (Optional)";
echo " this type can be set to \"t\" (plain text files) ";
echo " this type can be set to \"b\" (binary files) ";
echo " If the type is not provided it will be set to \"t\"";
echo "The attachement together with the type is 1 parameter you must provide both between quotes";
echo " example : \"attach1.txt t\" \"attach2.exe b\" \"attach3.xls b\"";
echo "----------------------------------------------------------------------------------------------------------------------";
echo "Example : mailto.sh ta \"test om te testen\" a_body.txt id079725@exchange.bc patrick@e-lelie.be \"attach.txt t\";";
echo "----------------------------------------------------------------------------------------------------------------------";
}
function gres
{
if [ $# -lt "3" ]
then
echo "Usage: gres pattern replacement file";
exit 1;
fi
pattern=$1;
replacement=$2;
if [ -f $3 ]
then
file=$3;
else
echo "$3 is not a file";
exit 1;
fi;
A="`echo | tr '\012' '\001' `";
sed -e "s$A$pattern$A$replacement$A" $file;
}
function striphtml
{
gres "<[^>]*>" " " $1 > $1.txt;
grep "<[^>]*>" $1.txt >/dev/null 2>&1;
while (( $? == 0 ))
do
gres "<[^>]*>" " " $1.txt > $1.tmp;
mv $1.tmp $1.txt;
grep "<[^>]*>" $1.txt >/dev/null 2>&1
done
}
function mailhead
{
echo "From: $1";
recipient=`echo $2 | tr -s " " | tr " " ";"`
echo "To: $recipient";
echo "Subject: $3";
echo "Mime-Version: 1.0";
}
function pack_attach
{
while (( $# > 1 ))
do
ATTACH=$1
ANAME=`basename $ATTACH`
ATYP=$2
echo "------NextPart"
echo "Content-Type: application/octet-stream;"
echo " name=\"$ATTACH\""
if [[ "$ATYP" = [bB] ]]
then
echo "Content-Transfer-Encoding: base64"
mpack -s "" -o $ATTACH.enc $ATTACH
lines=`cat $ATTACH.enc | wc -l`
x=0
while read line
do
(( x=$x+1 ))
if (( $x > 14 && $x < $lines - 1 ))
then
echo $line >> $ATTACH.tmp
fi
done < $ATTACH.enc
mv $ATTACH.tmp $ATTACH.enc
echo "Content-Disposition: attachment;"
echo " filename=\"$ANAME\""
echo ""
cat $ATTACH.enc
rm $ATTACH.enc
echo ""
shift
shift
else
if [[ "$ATYP" = [tT] ]]
then
echo "Content-Transfer-Encoding: 7 bit"
echo "Content-Disposition: attachment;"
echo " filename=\"$ANAME\""
echo ""
cat $ATTACH
echo ""
shift
shift
else
echo "Content-Transfer-Encoding: 7 bit"
echo "Content-Disposition: attachment;"
echo " filename=\"$ANAME\""
echo ""
cat $ATTACH
echo ""
shift
fi
fi
done
}
function textonly
{
echo "Content-Type: text/plain;";
echo " charset=\"ISO-8859-1\"";
echo "";
cat $1;
}
function htmlonly
{
echo "Content-Type: multipart/alternative;";
echo " boundary=\"----NextPart\"";
echo "";
echo "This message is in MIME format. Since your mail reader does not understand";
echo "this format, some or all of this message may not be legible.";
echo "";
echo "------NextPart";
echo "Content-Type: text/plain;";
echo " charset=\"ISO-8859-1\"";
echo ""
striphtml $1;
cat $1.txt;
rm $1.txt;
echo "------NextPart";
echo "Content-Type: text/html;";
echo " charset=\"ISO-8859-1\"";
echo "Content-Transfer-Encoding: quoted-printable"
echo "";
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
cat $1;
echo ""
echo "------NextPart--";
}
function textplus
{
echo "Content-Type: multipart/mixed;"
echo " boundary=\"----NextPart\""
echo ""
echo "This message is in MIME format. Since your mail reader does not understand"
echo "this format, some or all of this message may not be legible."
echo ""
echo "------NextPart"
textonly $1
echo ""
shift
pack_attach $@
echo "------NextPart--"
}
function htmlplus
{
echo "Content-Type: multipart/mixed;"
echo " boundary=\"----NextPart\""
echo ""
echo "This message is in MIME format. Since your mail reader does not understand"
echo "this format, some or all of this message may not be legible."
echo ""
echo "------NextPart"
htmlonly $1
echo ""
shift
pack_attach $@
echo "------NextPart--"
}
function sendFile
{
mailtje=mail`date +%H%M%S%d%m%m%y`.msg
mailhead $FROM_ADDRES "$TO_ADDRES" "$SUBJECT" > $mailtje;
case $TYPE in
t) textonly $BODY >> $mailtje;;
h) htmlonly $BODY >> $mailtje;;
ta) textplus $BODY $ATTACHMENTS >> $mailtje;;
ha) htmlplus $BODY $ATTACHMENTS >> $mailtje;;
esac;
cat $mailtje | /usr/sbin/sendmail $TO_ADDRES
rm $mailtje
}
# Assign parameters
####################
TYPE=`lower $1`;
SUBJECT=$2;
BODY=$3;
TO_ADDRES=$4;
FROM_ADDRES=$5;
# Check parameters
##################
x=0
if (( $# >= $min_para ))
then
echo "Number of parameters is acceptable" > $log
else
echo "Wrong number of parameters" >> $log
echo "$# parameters given" >> $log
echo "Given Parameters : $@ " >> $log
(( x=$x+1 ))
fi
if [[ "$TYPE" = "t" || "$TYPE" = "h" || "$TYPE" = "ta" || "$TYPE" = "ha" ]]
then
echo "Type has correct value" >> $log
else
echo "Type has incorrect value" >> $log
(( x=$x+1 ))
fi
if [[ -f $BODY ]]
then
echo "Body exists" >> $log
else
echo "Body doesn't exist" >> $log
(( x=$x+1 ))
fi
checka=`echo $TO_ADDRES | cut -d "@" -f1`
if [[ "$TO_ADDRES" = "$checka" ]]
then
echo "Adres is not valid" >> $log
(( x=$x+1 ))
else
echo "Adres is valid" >> $log
fi
if [[ "$FROM_ADDRES" = "" ]]
then
mach=`uname -n`
user=`whoami`
FROM_ADDRES=$user@$mach.bc
first_attach=5
else
if [[ -f `echo $FROM_ADDRES | cut -d" " -f1` ]]
then
mach=`uname -n`
user=`whoami`
first_attach=5
FROM_ADDRES=$user@$mach.bc
else
first_attach=6
fi
fi
par=1
while (( $par < $first_attach ))
do
(( par=$par+1 ))
shift
done
if [[ "$TYPE" = "ta" || "$TYPE" = "ha" ]]
then
ATTACHMENTS=""
while (( $# > 0 ))
do
if [[ -f `echo $1 | cut -d" " -f1` ]]
then
ATTACHMENTS=`echo "$ATTACHMENTS $1"`
else
echo "this attach \"$1\" is NOT found" >> $log
(( x=$x+1 ))
fi
shift
done
fi
if (( $x > 0 ))
then
Syntax >> $log
Error
fi;
# Send the mail
###################
sendFile
rm $log