How can I set up procmail to route my email to /dev/null if my mailbox
gets more than a certain number of messages in it?
Wellll, as someone else already pointed out this is dangerous, but, I
also believe in free choice. So, here goes.
Assuming that you meant your _system_ mailbox, here's how you can do
what you ask:
# Set MAX_MAIL to the maximum number of messages
MAX_COUNT=1000
# Count how many messages in ORGMAIL. This egrep fast, but possibly
# inaccurate; it can be fooled by having "From " at the start of
# lines in mail bodies. It just means it might count a few extra.
# BTW, the "test" is done to ensure that the maildrop really exists,
# otherwise, the grep or formail split will wait on STDIN.
:0
* ? test -f $ORGMAIL
{ COUNT=`egrep -c '^From ' $ORGMAIL` }
:0E
{ COUNT=0 }
# If accuracy is important, uncomment this recipe, and comment the
# one above.
# The slow way, but, the "procmail" way :^)
#:0
#* ? test -f $ORGMAIL
#{ XLIST=`formail -e -s echo -n x < $ORGMAIL`
# :0
# * XLIST ?? 1^1 x
# { COUNT = $= }
#}
#:0E
#{ COUNT=0 }
# Here's where the drop may occur
:0
* ? test $COUNT -gt $MAX_COUNT
/dev/null
Let's suppose that you would rather have a limit on the maximum size of
the mail, and drop that way.
MAX_SIZE=5000000 # 5 MBs maximum
:0
* ? test -f $ORGMAIL
{ SIZE=`wc -c <$ORGMAIL` } # get size of system mailbox
:0E
{ SIZE=0 }
NEW_SIZE=`wc -c` # get incoming mail size
TOTAL_SIZE=`expr $NEW_SIZE + $SIZE`
# If the total size exceeds the maximu, drop the mail
:0
* ? test $TOTAL_SIZE -gt $MAX_SIZE
/dev/null
Now, dropping the mail isn't very nice to your correspondents. You
should give them a choice of possibly sending it at a later time. So,
bounce it back with a little message...
# ... same preparation as above
# Check on the total size
:0
* ? test $TOTAL_SIZE -gt $MAX_SIZE
{
LOCKFILE=bounced.lock # protect the bounced.mail file
JUNK=`rm -f bounced.mail` # don't use old mail
:0c
bounced.mail # save a copy
# Generate a reply header
:0 fh
| formail -rt -I"From: $LOGNAME's Mail-Daemon <$LOGNAME>" \
-I"Subject: Mail not delivered" \
-I"X-Loop: $LOGNAME" \
-I"Precedence: junk" \
# Generate a reply body
:0 fb
| echo "Sorry, but your recent mail to $LOGNAME(_at_)$HOST (below)" ; \
echo "could not be delivered for some reason." ; \
echo "Your original message follows." ; \
echo "-----------------------------------------------------" ; \
cat bounced.mail
LOCKFILE # ok to trash the stored mail
# Deliver the composed bounce message
:0
! -oi -t
}
Hope this gives you a better understanding of procmail's versatility.
___________________________________________________________
Alan Stebbens <stebbens(_at_)sgi(_dot_)com> (415) 933-6437
Silicon Interactive Group, Silicon Graphics, Inc. (SGI)
M/S:9L991, 2011 N. Shoreline Blvd., Mountain View, CA 94043