imap_delete

(PHP 3, PHP 4, PHP 5)

imap_delete --  Mark a message for deletion from current mailbox

Description

bool imap_delete ( int imap_stream, int msg_number [, int options] )

Returns TRUE.

imap_delete() marks messages listed in msg_number for deletion. The optional flags parameter only has a single option, FT_UID, which tells the function to treat the msg_number argument as a UID. Messages marked for deletion will stay in the mailbox until either imap_expunge() is called or imap_close() is called with the optional parameter CL_EXPUNGE.

注: POP3 mailboxes do not have their message flags saved between connections, so imap_expunge() must be called during the same connection in order for messages marked for deletion to actually be purged.

例子 1. imap_delete() example

<?php

$mbox
= imap_open("{your.imap.host}INBOX", "username", "password")
    or die(
"Can't connect: " . imap_last_error());

$check = imap_mailboxmsginfo($mbox);
echo
"Messages before delete: " . $check->Nmsgs . "<br />\n";

imap_delete($mbox, 1);

$check = imap_mailboxmsginfo($mbox);
echo
"Messages after  delete: " . $check->Nmsgs . "<br />\n";

imap_expunge($mbox);

$check = imap_mailboxmsginfo($mbox);
echo
"Messages after expunge: " . $check->Nmsgs . "<br />\n";

imap_close($mbox);
?>

See also: imap_undelete(), imap_expunge(), and imap_close().


add a note add a note User Contributed Notes
jemore at none dot com
12-Sep-2006 04:15
Following message from sokolov at phix dot de :
If you want to delete an individual message, the msg_number parameter should be a true int. If it is a string like "1 " (space one) or " 1 " (space one space), the message will not be deleted. Use something like intval(trim($msg_number)).
contact at wdschools dot com
08-Feb-2006 11:18
if your pop mail box is overloaded, here's a script to bulk delete messages. fyi, messages are stored from oldest to newest.

$server  = "mail.domain.com";
$username = "username";
$password = "password";
$max_delete = 100;
set_time_limit(300);

$pop3 = imap_open('{'.$server.':110/pop3}INBOX', $username, $password) or die("error");
$num = imap_num_msg($pop3);

for($i = 1; $i <= $num && $i <= $max_delete; $i++)
   imap_delete($pop3, $i);

imap_expunge($pop3);
imap_close($pop3);
jacky at jackyhome dot myvnc dot com
10-Nov-2003 05:42
// is not a complete code but enough to clear out an entire mailbox.
// hope this can save your time :-)

<?php

if (isset($_REQUEST['DoNow']))
{
 
# PULL ADDITIONAL FILES
 
include_once ("common.php");
 
 
$conn = @imap_open("\{$server/$serverType}Trash", $user, $pass)
  or die(
"Connection to folder failed");
 
 
$headers = @imap_check($conn);
  (
$headers->Nmsgs > 0) or die("Trash is empty already !");
 
 
// delete email(s)
 
@imap_delete($conn,'1:*');  // to clear out an entire mailbox.
 
@imap_expunge($conn);
  echo
"Trash is empty.";
 
 
imap_close($conn);
}
else
{
  echo
"<form name='formA' action='".$_SERVER['PATH_INFO']."' method='POST'>"; ?>
  Are you sure to empty trash ?
  <p>
  <input type="submit" value="Go Ahead" name="DoNow">&nbsp;
  <input type="button" value="Cancel" name="Cancel" onClick='javascript:self.history.go(-1)'></form></p>
<?php
} ?>
James G
10-Apr-2003 12:05
I had some major issues deleting emails using this function.  Using IIS 5.0 and a win based Mail Server, I could not delete the emails individually.

My script merely needed to check the emails and update the database for bounce backs, after which I simply wanted to erase all emails.

If imap_delete($mbox,$email->MsgNo) just isnt working for you, you can try using

   imap_delete($mbox,'1:*');

to clear out an entire mailbox.

Hope this helps cause it drove me insane for about 5 hours.  :)
phpnotes at kipu dot co dot uk
25-Sep-2002 10:15
If you use POP it may be easier to open your link with the CL_EXPUNGE flag to make sure the message is actually deleted without having to call imap_expunge all the time
http://www.ohardt.com
01-Jul-2002 10:42
I tried imap_delete with PHP Version 4.2.1 and I was not able to delete messages from my POP3 mailbox until I used a String as Msg No. Best is to use the MsgNo. returned from the imap_headerinfo procedure.

   $msg = imap_headerinfo($link, $n); 
  
   // Do something here
  
   imap_delete($link, $msg->Msgno); 
   imap_expunge ($link);
vksgeneric at hotmail dot com
18-Apr-2000 07:05
For all those people who ask "Why in the world can't I delete messages from POP3 boxes" - you need to expunge right after you delete. This flag is not persistent when dealing with POP3 boxes (compared to IMAP); thus if you had to reconnect to the POP3 server after you deleted something, but haven't expunged it yet, you loose it. In short, call expunge or explicitly close the box after you are done with marking messages for deletion.
sokolov at phix dot de
07-Apr-2000 09:28
In PHP4 msg_number can be string with messages numbers (like a '1,2,6') and/or range of messages (like a '1:*')