Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Aging out old email in mailboxes on server

Posted on 2003-11-20
6
Medium Priority
?
310 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 1000 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
The main intent of this article is to make you aware of ‘Exchange fail to mount’ error, its effects, causes, and solution.
In this video we show how to create an Address List 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 Organization >> Ad…
To show how to create a transport rule 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 Mail Flow >> Rules tab.:  To cr…

782 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