Solved

Efficient way to delete old mails

Posted on 2004-09-13
7
274 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

759 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