Solved

Aging out old email in mailboxes on server

Posted on 2003-11-20
6
292 Views
Last Modified: 2013-12-17
I am running Sendmail & users access mailboxes via POP3.  There are a couple of specific applications where the email must be left on the server.  I would like to have some sort of process that periodically ages out old email so that the boxes don't just accumulate mail forever.

I cannot seem to find any existing programs that do this, and am considering writing my own.  Before I start that journey, I was wondering if anyone has already done this?

0
Comment
Question by:drcheap
  • 4
  • 2
6 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 9797595
You could use the perl code below.  

#!/usr/bin/perl
#
# NAME
#       prune-mail - removes old mail from a Unix mbox file
#
# SYNOPSIS
#       prune-mail user-name
#
# DESCRIPTION
#       This utility removes mail older than some fixed number of days ($AGE
#       below) from a standard Unix mbox file, like those wriiten by Sendmail.
#       It must be run from with the mail spool area (typically /var/mail or
#       /var/spool/mail). If your mail spool isn't in one of those two
#       locations you'll need to modify the code.
#
#       This code also assumes that the group that owns the mbox files will
#       be 'mail', if that's not the case on your system change the code
#
# AUTHOR: Jim Levie (jim@entrophy-free.net)
#
use Cwd;

$AGE = 90;
$MBOXGRP = 'mail';

$cutoff = time()-$AGE*24*60*60;

if($#ARGV != 0)
{
  die "Usage - prune-mail user-name\n";
}
#
# Check to see if we are running in /var/mail or /var/spool/mail
#
$pwd = cwd();
if($pwd ne '/var/mail' && $pwd ne '/var/spool/mail'
0
 
LVL 3

Author Comment

by:drcheap
ID: 9801580
Looks like either EE limits the size of the post or the paste didn't get everything.

Also looks like you've already done exactly what I would do w/o and answer.  If you can email that to me, make it available on the web or FTP, etc.  I'd love to try it out and award some points accordingly.

Thanks!
0
 
LVL 40

Accepted Solution

by:
jlevie earned 250 total points
ID: 9801670
Might have been a cut-n-paste error on my part... I can see all of it in the comment box, lets see if it makes it into a comment.

#!/usr/bin/perl
#
# NAME
#       prune-mail - removes old mail from a Unix mbox file
#
# SYNOPSIS
#       prune-mail user-name
#
# DESCRIPTION
#       This utility removes mail older than some fixed number of days ($AGE
#       below) from a standard Unix mbox file, like those wriiten by Sendmail.
#       It must be run from with the mail spool area (typically /var/mail or
#       /var/spool/mail). If your mail spool isn't in one of those two
#       locations you'll need to modify the code.
#
#       This code also assumes that the group that owns the mbox files will
#       be 'mail', if that's not the case on your system change the code
#
# AUTHOR: Jim Levie (jim@entrophy-free.net)
#
use Cwd;

$AGE = 90;
$MBOXGRP = 'mail';

$cutoff = time()-$AGE*24*60*60;

if($#ARGV != 0)
{
  die "Usage - prune-mail user-name\n";
}
#
# Check to see if we are running in /var/mail or /var/spool/mail
#
$pwd = cwd();
if($pwd ne '/var/mail' && $pwd ne '/var/spool/mail'
   && $pwd ne '/home/mail-spool')
{
  die "prune-mail isn't being used within /var/mail or /var/spool/mail\n";
}
#
# Get the mbox owner's UID. If they aren't in the passwd file, punt.
#
$uid = getpwnam($ARGV[0]);
if($uid < 0) {die "Can't find $ARGV[0] in the passwd file\n";}
#
# Likewise for the mbox group.
#
$gid = getgrnam($MBOXGRP);
if($gid < 0) {die "Can't find $MBOXGRP in the group file\n";}
#
# Okay, open the user's mbox file and create a new file for the messages
# we keep.
#
open(INP, "<$ARGV[0]") || die "Can't open <$ARGV[0]>\n";
open(OUP, ">$ARGV[0].new") || die "Can't create <$ARGV[0].new>\n";
#
# Now walk the mbox file. Keep all messages with a date less than
# $AGE days ago
#
$prev_nl = 1;
$keep = 1;
while(<INP>)
{
  if($prev_nl && /^From \S+@\S+  (Sun|Mon|Tue|Wed|Thu|Fri|Sat)/)
  {
    ($t1, $t2, $t3, $date) = split(/\s+/, $_, 4);
    chop($date);
    $this = `date -d "$date" +%s`;
    if($this =~ /\d+/ && $this >= $cutoff)
    {
      $keep = 1;
    }
    else
    {
      $keep = 0;
    }
  }
  $this = $_;
  chop($this);
  $prev_nl = (length($this) == 0 ) ? 1:0;
  if($keep) {print OUP $_;}
}
close(INP);
close(OUP);
$res = rename("$ARGV[0].new", $ARGV[0]);
if(!$res) {die "Failed to rename $ARGV[0].new to $ARGV[0]\n";}
#
# Make sure that the file ownship and mode is correct
#
$res = chown($uid, $gid, $ARGV[0]);
if(!$res) {die "Failed to set ownership of $ARGV[0] to $uid:$gid\n";}
$res = chmod(0660, $ARGV[0]);
if(!$res) {die "Failed to set perms of $ARGV[0] to rw-rw----\n";}
print "$ARGV[0]\n";
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 40

Expert Comment

by:jlevie
ID: 9801672
Yep, that worked.
0
 
LVL 3

Author Comment

by:drcheap
ID: 9803111
Indeed.  I ran a few tests in a sandbox and it worked perfectly.  Thank you very very very much!
0
 
LVL 40

Expert Comment

by:jlevie
ID: 9803730
Just remember that that coode is pretty simple in that it takes no precautions about the mbox being modified by a client or sendmail when it is pruning the mbox. Be sure to run it at a slack time, or better yet shut down the MTA and client access when pruning an mbox.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

This short article will present "How to import ICS Calendar onto Office 365 Calendar". I was searching for free (or not free) tools to convert ICS to CSV without success. The only tools I found & working well were online tools...this was too hard to…
Granting full access permission allows users to access mailboxes present in their database. By giving full access permission one can open and read the content of any mailbox but cannot send emails from that mailbox.
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now