Solved

Aging out old email in mailboxes on server

Posted on 2003-11-20
6
294 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When it comes to providing great business solutions, IBM and Microsoft are the two top companies excelling in the art. Both launch similar products aimed at a wide audience set and have a good customer satisfaction rate. Since their products are qui…
New-MailboxSearch Powershell Command and step by step approach to Search and Extract Emails form Exchange 2013 Journaling server.
In this video we show how to create an Accepted Domain 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 >> Ac…
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager

930 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

12 Experts available now in Live!

Get 1:1 Help Now