Page 1 of 1
This code does not delete all the mails why ?
Posted: Thu Nov 07, 2002 9:13 am
by suhailkaleem
<?
$link = imap_open($mailserver, $username, $password);
$total = imap_num_msg($link);
imap_close($link);
$x=0;
for ( $i=1; $i < $total;$i++)
{
$link1 = imap_open($mailserver, $username, $password);
imap_delete($link1, $x);
$x++ ;
}
imap_expunge($link1);
imap_close($link1);
//The above code should delete all the mails on the mail server but it does not ???
//it some time delete one or some times 2 but not all of them
i am using windows(iis , and MDEAMON mail server )
?>
Posted: Thu Nov 07, 2002 9:31 am
by volka
why do you reconnect in every loop?
Posted: Thu Nov 07, 2002 9:48 am
by suhailkaleem
This also does't work
<?
$link = imap_open($mailserver, $username, $password);
$total = imap_num_msg($link);
imap_close($link);
$x=0;
$link1 = imap_open($mailserver, $username, $password);
for ( $i=1; $i < $total;$i++)
{
imap_delete($link1, $x);
$x++ ;
}
imap_expunge($link1);
imap_close($link1);
//The above code should delete all the mails on the mail server but it does not ???
//it some time delete one or some times 2 but not all of them
i am using windows(iis , and MDEAMON mail server )
?>
Posted: Thu Nov 07, 2002 9:55 am
by seg
Code: Select all
<?php
$link = imap_open($mailserver, $username, $password); // connect to imap
$total = imap_num_msg($link); // get number of messages
imap_close($link); // disconnect from imap
$x=0;
$link1 = imap_open($mailserver, $username, $password); // link to imap... again.
for ( $i=1; $i < $total;$i++) // set $i to 1, as long as it is less than $total, add 1 to $i, but set it back to 1 at the start of each loop...
{
imap_delete($link1, $x); // delete the messages marked at $x (why not $i ?)
$x++ ; // add 1 to $x
}
// clean up
imap_expunge($link1);
imap_close($link1);
?>
I'm not wizard at imap function, frankly ive never used it before. But you take a look at that code and tell me whats wrong.
Posted: Thu Nov 07, 2002 10:10 am
by volka
Never used imap before, too

$i=1; $i < $total;$i++
I think you miss one message.
a little error-logging might be useful.
Code: Select all
<?php
$link = imap_open($mailserver, $username, $password) or die('connection failed');
$total = imap_num_msg($link);
// debugInfo
oInfo = imap_mailboxmsginfo($link);
echo 'msgs: ', oInfo->Nmsgs, '<br/>';
// delete-loop
$i=0;
while($i < $total)
imap_delete($link, ++$i);
//debugInfo
echo 'deleted: ', oInfo->Deleted, '<br/>';
echo 'errors: ';
print_r(imap_errors());
echo '<br/>alerts: ';
print_r(imap_alerts());
// close connection after expunge
imap_close($link, CL_EXPUNGE);
?>