[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Efficient way to delete old mails

Posted on 2004-09-13
7
Medium Priority
?
324 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

872 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