Why imap_delete really delete the message instead of marking it for deletion?

Have anyone used imap_delete before? I’m trying to mark a message for deletion with imap_delete but instead it was really deleting the message. I checked in squirrelmail it’s gone from inbox. When I executed the code again it goes straight to die(“Couldn’t get emails”) when calling imap_headers. And printing imap_errors returns empty.

Is there some setting I need to configure first? Is there any way to mark a message for deletion?

Thanks :slight_smile:


$conn = imap_open($this->imapPath."INBOX",$this->username,$this->password) or die("Connection to server failed");
$headers = imap_headers($conn) or die("Couldn't get emails");
$mailnumbers = imap_search($conn,"UNDELETED");

foreach($mailnumbers as $i) {
  $mailHeader = @imap_headerinfo($conn, $i);
  $mailfrom = $mailHeader->from;
  $objfrom = $mailfrom[0];
  $from = $objfrom->mailbox . "@" . $objfrom->host;

  $body = imap_body($conn,$i);
  // ...store message body to db
  imap_delete($conn, $i);
}
imap_close($conn);

If you change it to

$conn = imap_open($this->imapPath."INBOX",$this->username,$this->password) or die("Connection to server failed");

$headers = imap_headers($conn) or die("Couldn't get emails");
$mailnumbers = imap_search($conn,"UNDELETED");

foreach($mailnumbers as $i) {
  $mailHeader = @imap_headerinfo($conn, $i);
  $mailfrom = $mailHeader->from;
  $objfrom = $mailfrom[0];
  $from = $objfrom->mailbox . "@" . $objfrom->host;

  $body = imap_body($conn,$i);
  // ...store message body to db
  imap_delete($conn, $i);
}

foreach($mailnumbers as $j) {
  imap_undelete($conn, $j);
}

imap_close($conn); 

do they “reappear” in SquirrelMail? Have they been deleted or are they inside some kind of “email purgatory”?

Hi Mittineague,

When I add imap_undelete like you suggested, the messages didn’t get deleted, but the old ones that were accidently deleted through imap_delete still won’t show up. I also check the qmail directories under cur, new, tmp, .Trash/cur, .Trash/new, .Trash/tmp, they’re not there. It’s as if the imap_close automatically calls imap_expunge before actually closing the connection.

I wonder if anyone had this experience before, and whether this is a PHP problem or a mail server/web hosting problem?

I’m guesing it must be some kind of server clean-up (CRON job?) the host has running. AFAIK the only way imap_delete() should actually delete is if a third argument of CL_EXPUNGE is passed to it. If the constant’s value is 32768 then it seems like even if your code was written wrong ($conn, $i, $i) and $i reached that, even then only the one should actually get deleted.

No, I think your host is running a clean-up script.

OK, I’ll submit a ticket to ask my webhost about this. Thanks for your help so far :slight_smile: