Scott Wiersdorf
Created: Thu Apr  8 23:23:58 MDT 2004
$Id: index.html,v 1.1 2005/08/15 18:25:30 scott Exp $
mail_newer - the mail_newer script
mail_newer2 - the mail_newer2 script

mail_newer prints a mail message if it is newer than a specified number of days. mail_newer is non-destructive: it does not operate on mailboxes directly, but prints matching messages to stdout.

mail_newer version: 1.2
mail_newer2 version: 1.2


Often you'll have a mailbox in mbox format that you want to delete messages older than a certain number of days. You can use formail (from the procmail toolkit) and mail_newer to do this (assuming mail_newer is in your path):

formail -s mail_newer 7 < /home/foo/mbox > /home/foo/ contains the messages that have been received in the past 7 days.

To run from cron on a shared hosting platform (e.g., viaVerio Signature), do this (courtesy Sherman Pierce):

    23 5 * * * formail -s $HOME/path/to/mail_newer 10 < $HOME/users/joe/mail/Trash > $HOME/users/joe/mail/Trash2 && mv $HOME/users/joe/mail/Trash2 $HOME/users/joe/mail/Trash
or on a VPS v2 (in /etc/crontab):
    23 5 * * * joe_user formail -s /path/to/mail_newer 10 < $HOME/Mail/Trash > $HOME/Mail/Trash2 && mv $HOME/Mail/Trash2 $HOME/Mail/Trash 


If you use mail_newer on live mailboxes (i.e., a mailbox where mail might be delivered to at any time), there is a slight risk of clobbering new mail messages that arrived between the time of processing and the time you execute the 'mv' command. You can minimize this risk by executing them in the same subshell:
formail -s mail_newer 7 < mbox > && mv mbox
but the race condition still exists (though is very small). mail_newer is safe to run on mail archives and other non-live mailboxes.


Place mail_newer (or mail_newer2) in /usr/local/sbin and make executable (chmod 555 /usr/local/sbin/mail_newer).


mail_newer2 is a slightly more compact (and uses safer date parsing) version of mail_newer; it uses HTTP::Date (and possibly Time::Zone) Perl modules, so they must already be installed on your system.


I am currently (17 Dec 2004) not recommending mail_newer2 because it seems to handle badly formatted dates, well, badly. mail_newer should work just fine for most mailboxes.

System Administration tutorials

Send comments to the author