Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Efficient way to delete old mails

Posted on 2004-09-13
7
Medium Priority
?
321 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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

636 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