Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Aging out old email in mailboxes on server

Posted on 2003-11-20
6
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
As cyber crime continues to grow in both numbers and sophistication, a troubling trend of optimization has emerged over the last year.
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…
In this video we show how to create an email address policy 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…

670 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