Solved

Efficient way to delete old mails

Posted on 2004-09-13
7
285 Views
Last Modified: 2009-07-29
I have a script which does a bunch of mail processing, including deleting old mails from certain IMAP folders. I currently use the following to do this:-

            for ($msg = 1 ; $msg <= $numsg ; ++$msg) {
                  $header = imap_headerinfo($mbox, $msg);
                     $date = $header->udate;
                  if (($date + $age) < $now) imap_delete($mbox,$msg);
            }

            imap_expunge($mbox);

This is run for a number of different folders, many of which have rather a lot (>1000) messages, so going through and checking the date of every single message and deleting one by one strikes me as a rather inefficient way to do this.

Could someone supply me with a rather more efficient bit of code to replace the above code with?

thanks

mark.
0
Comment
Question by:markauk
  • 3
  • 3
7 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 12052030
One way could be to sort the mails by time, and then start deleting,
In this way you will not need to check the newer files
0
 
LVL 9

Expert Comment

by:techtonik
ID: 12083902
      $thold = $now - $age;
       for ($msg = 1 ; $msg <= $numsg ; ++$msg) {
               $header = imap_headerinfo($mbox, $msg);
               if ($header->udate < $thold) imap_delete($mbox,$msg);
          }
       imap_expunge($mbox);

----
       Or use imap_sort, then imap_headerinfo with array_slice to get the sequence and after that imap_setflag_full to set \\Deleted flag

http://www.php.net/imap_setflag_full 
http://www.php.net/array_slice
http://www.php.net/imap_headerinfo
http://www.php.net/imap_sort
0
 

Author Comment

by:markauk
ID: 12083967
Not sure how that code is really any different from my own.

The suggestion of using sort, sounds better if someone can supply my with the required code. Or, is there a way to read dates of all messages in a folder into an array in one go, figure out which ones are old, and then delete all the relevant messages in one go?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:techtonik
ID: 12087803
It is just a bit faster PHP code.
I don't have many email account with thousands of useless letters, so you will have to test this yourself on another mailbox. =)

// Let's start with
$msgnumbersbydate = imap_sort($mbox, SORTDATE, 1);

// now you've got message numbers, sorted by date in reverse order
// next find first element in array, from which queue has to be deleted
$thold = $now - $age;
reset($msgnumbersbydate);
while (list($key, $value) = each($msgnumbersbydate)) {
    $header = imap_headerinfo($mbox, $msg);
    if ($header->udate < $thold) {
        $delfrom = $key;
        break;
    }
}

// if there's something to delete build mesage sequence and set \\Deleted flag
if (isset($delfrom)) {
    // building sequence string
    $seqarray = array_slice($msgnumbersbydate, $delfrom);
    $seqstring = implode(',', $seqarray);

    // flag messages as deleted
    imap_set_flag_full ($mbox, $seqstring, "\\Deleted");
    // call expunge to actually delete them
    imap_expunge($mbox);
}

Should work. Please, let us know if something goes wrong or if you will make some modifications to this code to work.
0
 

Author Comment

by:markauk
ID: 12188416
Looks like this should be an improvement, but doesn't reverse order sorting mean that the newest message comes first? Would probably be faster to start with oldest message... as long as I run the script regularly there will always be more messages not to delete than to delete.

Also, any reason you did the deletes the way you did rather than just imap_delete($mbox,'1:$key') (assuming we are sorted oldest first)?
0
 
LVL 9

Accepted Solution

by:
techtonik earned 500 total points
ID: 12189127
Yes, I think reversing order is newest first. But it's easy to test:

reset($msgnumbersbydate);
while (list($key, $value) = each($msgnumbersbydate)) {
    $header = imap_headerinfo($mbox, $value);
    echo $header->date." <br/>\n";
}

It is a good idea to start from the oldest. It will be easy to modify (just fix error with $msg instead of $value in my previous post).

As for imap_delete.. Well, imap_sort returns an array of message numbers sorted by given criteria. I suppose, that actual messages in mailbox are not sorted by this function and message numbers do not depend on message dates. It will be interesting to test though:

reset($msgnumbersbydate);
while (list($key, $value) = each($msgnumbersbydate)) {
  echo $value." </br>\n";
}

Will be looking for results from you. =)
0
 

Author Comment

by:markauk
ID: 12190667
You are right, imap_sort doesn't actually sort the messages, so the simple imap_delete won't work. Here's what I've ended up going with:-

            $msgnumbersbydate = imap_sort($mbox, SORTARRIVAL, 0);

            reset($msgnumbersbydate);
            $delete_msgs = "";
            while (list($key, $msg) = each($msgnumbersbydate)) {
                  $header = imap_headerinfo($mbox, $msg);
                  if ($header->udate < $oldest) {
                        $delete_msgs .= "$msg,";
                  } else {
                        break;
                  }
            }

            if ($delete_msgs<>"") {
                  $delete_msgs = rtrim($delete_msgs,",");
                  imap_delete($mbox,$delete_msgs);
                  imap_expunge($mbox);
            }
            imap_close($mbox);

It's not quite as super efficient as I was hoping, but certainly better than what I had.

Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

20 Experts available now in Live!

Get 1:1 Help Now