Solved

Aging out old email in mailboxes on server

Posted on 2003-11-20
6
301 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hosting email transfer 3 45
SMTP Service 9 59
Exchange 2010 to 2013 8 75
Phishing emails coming from Staff-A email account to another external email account 10 29
Automapping, a wonderful feature with Exchange 2010 (SP2 onwards I believe), allows additional/Shared mailboxes that a user has access to be automatically mapped on Outlook client, simplifying the process by adding them while Outlook launches. Ho…
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
In this video we show how to create a Contact 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 Recipients >> Contact ta…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

832 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