"C" == Contractor <Vikas> writes:
C> Hi there,
C> I have a question on sendmail. The mailbox on some of the
C> accounts is growing at an exponential scale. I want to be able
C> to delete 30 day old messages from the mailbox.
C> Any suggestions on how to do it
I wrote a shell script awhile ago to do something like this using
formail. It's probably not the most efficient/effective way to do it
though, so I'd welcome any feed back on it. For one thing, now that I
look at it again, and after reading this list for awhile, I'm not sure
that I'm getting the date of the message very reliably.
#!/bin/sh
# expire_mail: Written by William Avery, March 1996
# This script will delete messages older than $AGE days from the
# mailbox specified on the command line. It requires that you
# have formail installed on your system, and if formail is in a
# directory other than /usr/bin, you must change the value of
# $FORMAIL below.
TEMPMSG=message.tmp
TEMPMBOX=mailbox.tmp
AGE=5
FORMAIL=/usr/bin/formail
if [ ! -x ${FORMAIL} ]; then
echo This script requires ${FORMAIL}, which comes with procmail.
exit 1
elif [ ! "${1:-}" ]; then
echo "Usage: $0 <mailbox path>"
exit 1
elif [ -f "${TEMPMSG}" ]; then
echo "${TEMPMSG} will be destroyed."
echo -n "Continue? (y/n): "
read RESPONSE
case ${RESPONSE} in
[yY]*)
echo Continuing....
;;
*)
echo Good Bye.
exit
;;
esac
fi
if [ "${1}" != "select" ]; then
if [ -f "${TEMPMBOX}" ]; then
echo "${TEMPMBOX} will be destroyed."
echo -n "Continue? (y/n): "
read RESPONSE
case ${RESPONSE} in
[yY]*)
echo Continuing....
;;
*)
echo Good Bye.
exit
;;
esac
fi
rm -f $TEMPMBOX
${FORMAIL} -s $0 select < $1
mv -i $TEMPMBOX $1
exit
fi
TODAY=`date -d "\`date\`" +"%Y%j"`
EXPIRATION=`expr ${TODAY} - ${AGE}`
cat > $TEMPMSG
MESSAGE=`sed -n -e 's/^Date:\(.*\)$/\1/p' ${TEMPMSG} | head -n 1`
MESSAGE=`date -d "${MESSAGE}" +"%Y%j"`
if [ $MESSAGE -ge $EXPIRATION ]; then
cat $TEMPMSG >> $TEMPMBOX
fi
rm -f $TEMPMSG