downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imap_deletemailbox> <imap_createmailbox
[edit] Last updated: Fri, 11 Nov 2011

view this page in

imap_delete

(PHP 4, PHP 5)

imap_delete現在のメールボックスから削除するメッセージに印を付ける

説明

bool imap_delete ( resource $imap_stream , int $msg_number [, int $options = 0 ] )

msg_number が指すメッセージに 削除予定のマークをします。削除マークを付けられたメッセージは、 imap_expunge() がコールされるか imap_close()CL_EXPUNGE を付けてコールされるかのどちらかが行われるまでメールボックスに残ったままになります。

パラメータ

imap_stream

imap_open() が返す IMAP ストリーム。

msg_number

メッセージ番号。

options

FT_UID を指定すると、 引数 msg_numberUID として処理することを関数に指示できます。

返り値

Returns TRUE.

例1 imap_delete() の例

<?php

$mbox 
imap_open("{imap.example.org}INBOX""username""password")
    or die(
"接続できません: " imap_last_error());

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

imap_delete($mbox1);

$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);
?>

注意

注意:

POP3 メールボックスは、コネクション間で保存可能なメッセージフラグを持っていません。 そのため、削除マークをつけたメッセージを本当に削除するためには、 同一の接続内で imap_expunge() をコールする必要があります。

参考

  • imap_undelete() - 削除マークがついているメッセージのマークをはずす
  • imap_expunge() - 削除用にマークされたすべてのメッセージを削除する
  • imap_close() - IMAP ストリームをクローズする



imap_deletemailbox> <imap_createmailbox
[edit] Last updated: Fri, 11 Nov 2011
 
add a note add a note User Contributed Notes imap_delete
thisemailaddress at gmail dot com 24-Sep-2009 03:18
In case you feel the need to kill all emails from before a certain year on your Gmail account, this would work:

<?php
error_reporting
(E_ALL ^ (E_NOTICE | E_WARNING));

echo
"parsing ini file...\n";
$ini = parse_ini_file('g.ini');
$user = $ini['user'];
$pass = $ini['pass'];
$year = $ini['year'];
echo
"account {$user} - killing msgs from before {$year}\n\n";

echo
"connecting...\n";
$imap = imap_open("{imap.gmail.com:993/imap/ssl/novalidate-cert}[Gmail]/All Mail", $user, $pass) or die("can't connect: " . imap_last_error() . "\n");

echo
"checking current mailbox...\n";
$mbox = imap_check($imap);

echo
"fetching overview...\n";
$flaggedForDelete = 0;
for(
$n = 0; $n < $mbox->Nmsgs; $n++) {
    echo
"processing {$n} of {$mbox->Nmsgs}...\r";
   
$hdr = imap_fetchheader($imap, $n);
   
preg_match('/^Date: (.*?)$/m', $hdr, $matches);
   
$date = date_parse(strtotime($matches[1]));
    if(
$date['year'] < $year) {
       
imap_delete($imap, $msg->msgno);
       
$flaggedForDelete++;
    }
}

echo
"expunging mailbox ({$flaggedForDelete} messages flagged)... ";
echo ((
imap_expunge($imap)) ? "ok" : "failed!")."\n";
imap_close($imap);

?>
Alex 24-Apr-2009 04:30
I think you don't have to give a range to imap_delete() if you want to delete a single one.

This works well:

<?php

imap_delete
($conn,trim($msgno));   

?>
chris at financialservicesonline dot com dot au 29-Jan-2009 10:24
After a little experimentation i found out how to mark a single message for deletion...

<?php

$msgid
= '5'; //For example

imap_delete($mbox, "$msgid:$msgid");

?>

imap_delete() seems to want a range, so to select one, simply range from your id to your id.

Hope this helps.
iwantsimplelife at lycos dot com 12-Jan-2009 08:10
Somehow, when ever I try to delete an email using the message number returned by imap_header, any email with a message number below 999 will fail.

I discovered that the imap_header will pad the message number with spaces. You will need to trim the number before calling imap_delete.

I am using qmail and it took me over a month to figure it out.

Hope this will be helpful to some poor soul out there facing the same problem.
jacky at jackyhome dot myvnc dot com 10-Nov-2003 08:12
// 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 01:35
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.  :)

 
show source | credits | sitemap | contact | advertising | mirror sites