imap_expunge

(PHP 3, PHP 4, PHP 5)

imap_expunge -- Delete all messages marked for deletion

Description

bool imap_expunge ( resource imap_stream )

imap_expunge() deletes all the messages marked for deletion by imap_delete(), imap_mail_move(), or imap_setflag_full().

Returns TRUE.


add a note add a note User Contributed Notes
boswachter at xs4all dot nl
12-Jan-2006 11:57
@eisbrenner at gidn dot de

You shouldn't call imap_expunge until before closing the connection. imap_delete tags a message for deletion, imap_expunge deletes all tagged messages. i.e.:
for ($i = 0; $i < $num; $i++) {
  imap_delete($box, $i);
}
imap_expunge($box);

imap_expunge should not be in your inner loop.
eisbrenner at gidn dot de
06-Jan-2006 06:56
Be careful using this function with a pop3 server, if you have an array of mails you want to handle in a loop.

If you want wo handle the mailcontent to use it in your application and use i.e.

$messages = imap_sort($connection, SORTARRIVAL, FALSE);

and have an loop like
foreach($messages as $i) {
   // do something with the content
   imap_delete($i);
   imap_expunge($connection);
}
every 2nd mail will be left untouched.
the first will be read, deleted, the second becomes #1, but due to the loop-condition #2, which was #3 as you started, will be left and #4 - which is #3 now, will be read instead.

the pop3-protocol has no expunge-method, so i assume they disconnect to delete the flagges mails and reconnect.

:-((

So you have to you the number 1 for all ....