Dutch PHP Conference 2025 - Call For Papers

imap_delete

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_deleteBelirtilen iletiyi silindi olarak imler

Açıklama

imap_delete(IMAP\Connection $imap, string $ileti_num, int $seçenekler = 0): true

ileti_num ile belirtilen iletileri silinmek üzere imler. Bu iletiler bir imap_expunge() çağrısına kadar veya CL_EXPUNGE seçeneği ile yapılan bir imap_close() çağrısına kadar posta kutusunda kalır.

Bağımsız Değişkenler

imap

IMAP\Connection nesnesi.

ileti_num

IMAP4 tarzı aralık dizgesi biçeminde bir veya daha fazla ileti içeren bir dizge ("n", "n:m" veya bunların virgüllerle ayrılmış birleşimleri).

seçenekler

Bu bağımsız değişkende FT_UID sabiti belirtilirse ileti_num bağımsız değişkeninin eşsiz kimlikleri içerdiği varsayılır.

Dönen Değerler

Daima true döndürür.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 seçenekler geçersiz ise artık ValueError oluşuyor. Evvelce, bir uyarı verilir ve işlev false döndürürdü.
8.1.0 imap bağımsız değişkeni artık IMAP\Connection nesnesi kabul ediyor, evvelce resource türünde geçerli bir imap değeri kabul ederdi.

Örnekler

Örnek 1 - imap_delete() örneği

<?php

$mbox
= imap_open("{imap.example.org}INBOX", "username", "password")
or die(
"bağlanılamadı: " . imap_last_error());

$check = imap_mailboxmsginfo($mbox);
echo
"Silme imlemesi öncesi ileti sayısı: " . $check->Nmsgs . "<br />\n";

imap_delete($mbox, 1);

$check = imap_mailboxmsginfo($mbox);
echo
"Silme imlemesi sonrası ileti sayısı: " . $check->Nmsgs . "<br />\n";

imap_expunge($mbox);

$check = imap_mailboxmsginfo($mbox);
echo
"İletiler silindikten sonraki ileti sayısı: " .
$check->Nmsgs . "<br />\n";

imap_close($mbox);
?>

Notlar

Bilginize:

IMAP posta kutuları bağlantılar arasında ileti imlerini saklamazlar. Bu nedenle silinmek üzere imlenmiş iletileri gerçekten silmek için imap_expunge() işlevini aynı oturumda kullanmalısınız.

Ayrıca Bakınız

add a note

User Contributed Notes 10 notes

up
7
erational
11 years ago
the function is using the message number (and not the uid !)
$uid = $mail->uid;
$msgno = $mail->msgno;

if you want to use the uid, simply option value
imap_delete($mbox, $uid, FT_UID);
up
4
rueda dot raul at gmail dot com
10 years ago
This function seems not to work on "[Gmail]/All Mail" mailbox. This is because Gmails keeps only one copy of the message and mailboxes are no more than labels.
Deleting a message from a mailbox only removes that label (except for "All Mail" mailbox).
To delete from "All Mail" you have to use imap_mail_move() function and move the message to "[Gmail]/Trash" mailbox.

Note: Names of mailboxes in Gmail depends on the language you're using. E.g.: in Spanish "All Mail" is "Todos" and "Trash" is "Papelera".
up
1
jacky at jackyhome dot myvnc dot com
20 years ago
// 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
} ?>
up
1
Daniel Dafoe
8 years ago
Just so anyone using this function is aware: if you mark an email to be deleted from a mailbox, that flag does not persist. You need to call imap_expunge() or use the CL_EXPUNGE option with imap_close() to remove the marked email in the same PHP script you marked it in.

I hope this helps someone out.
up
0
Olle
11 years ago
I couldn't get imap_delete to work when I used it with pop3. Instead i wrote:
<?php
function pop3_dele($connection,$message)
{
$status = imap_setflag_full($connection, '1:'.$message, '\\Deleted');
imap_expunge($connection);
return
$status;

//return(imap_delete($connection,trim($message)));
}
?>
up
0
chris at financialservicesonline dot com dot au
15 years ago
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.
up
0
iwantsimplelife at lycos dot com
15 years ago
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.
up
0
James G
21 years ago
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. :)
up
-1
thisemailaddress at gmail dot com
14 years ago
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);

?>
up
-3
Alex
15 years ago
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));

?>
To Top