Sendmail queue cleanup

When a subscribers mail box fills up or they exceed their quota the mail starts to pile up in the /var/spool/mqueue directory. A search of the Internet found several scripts that will clean it up but most use qtool.pl which I haven’t been able to find. Typical is http://www.brandonhutchinson.com/deleting_mail_queue.html . I did find a ksh script SQG – Sendmail Queue Groomer at http://www.sun.com/bigadmin/scripts/submittedScripts/queue_groomer.ksh.txt that was a starting point for the following.
Note: Jan, 2010 – make sure that the /var/spool/morgue-queue directory exists before running the script or the files will get moved to a file of that name instead of being moved into that directory.

#!/bin/bash
#-----------------------------------------------------------------------#
# SQG - Sendmail Queue Groomer
#
#       David G. Sullivan
#       dsullivan@grapevinesolutions.com
#       12/28/2003
#
# Modified for Redhat - Linux / bash
#
#	Alan Madill
#	amadill@hwy16.com
#	08/11/2009
#
#  Description:
#  A very simple script to keep normal sendmail queues clear of
#  undeliverable email.  Undeliverable email is moved to a separate
#  queue (morgue-queue) for processing from cron. Mail in the
#  morgue-queue is removed after X attempts at delivery.
#
#
#  Add the below command in cron to run every 30 minutes to process mail
#  in the morgue directory with a separate queue runner.
#
#  /usr/lib/sendmail -q -O QueueDirectory=/var/spool/morgue-queue
#-----------------------------------------------------------------------#

#-----------------------------------------------------------------------#
# Operating parameters
#-----------------------------------------------------------------------#
declare -i queue_max=10    # Max delivery attempts for normal mail before
                                      # moving mail to morgue directory.

declare -i morgue_max=15   # Max delivery attempts for morgue mail before
                                       # deleting mail message.


#-----------------------------------------------------------------------#
# Command locations
#-----------------------------------------------------------------------#
SENDMAIL=/usr/lib/sendmail
DATE=/bin/date
GREP=/bin/grep
CUT=/bin/cut
MV=/bin/mv
RM=/bin/rm
LS=/bin/ls

#-----------------------------------------------------------------------#
# Directory locations
#-----------------------------------------------------------------------#
Mail_Queues=/var/spool/mqueue
Mail_Morgue=/var/spool/morgue-queue

#-----------------------------------------------------------------------#
# Log file for morgue transactions (Mail delivery is still in syslog)
#-----------------------------------------------------------------------#
logfile=/var/log/morgue.log

#-----------------------------------------------------------------------#
# Stop sendmail
#-----------------------------------------------------------------------#

/sbin/service sendmail stop
/bin/sleep 5

#-----------------------------------------------------------------------#
# Check Mail Queues
#-----------------------------------------------------------------------#
for y in $($LS $Mail_Queues|$GREP ^qf*)
  do
         #echo "file" $y
         declare -i delv_attempts=$($GREP ^N $Mail_Queues/$y|$CUT -c 2-5)
         msgid=$(echo $y|$CUT -c 3-35)
         date=$($DATE)
         To=$($GREP ^rRFC822 $Mail_Queues/$y|awk '{print $2}')
         if (test $delv_attempts -ge $queue_max) then
           echo $date", MSGID: "$msgid" Moved, "$To >> $logfile
           #echo $MV $Mail_Queues"/qf"$msgid" "$Mail_Morgue
           $MV $Mail_Queues/qf$msgid $Mail_Morgue
           #echo $MV $Mail_Queues"/df"$msgid" "$Mail_Morgue
           $MV $Mail_Queues/df$msgid $Mail_Morgue
         fi
  done

#-----------------------------------------------------------------------#
# Check Morgue Mail Queue
#-----------------------------------------------------------------------#
for y in $($LS $Mail_Morgue|$GREP ^qf*)
  do

      declare -i delv_attempts=$($GREP ^N $Mail_Morgue/$y|$CUT -c 2-5)
      msgid=$(echo $y|$CUT -c 3-35)
      date=$($DATE)
      To=$($GREP ^rRFC822 $Mail_Morgue/$y|awk '{print $2}')
      if (test $delv_attempts -ge $morgue_max) then
          echo $date", MSGID: "$msgid" Removed, "$To >> $logfile
          #echo $RM $Mail_Morgue"/qf"$msgid
          $RM $Mail_Morgue/qf$msgid
          #echo $RM $Mail_Morgue"/df"$msgid
          $RM $Mail_Morgue/df$msgid
      fi

  done

#-----------------------------------------------------------------------#
# Start sendmail
#-----------------------------------------------------------------------#

/sbin/service sendmail start

##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed. 
###
###
###  Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.jsp
##############################################################################