This code does not delete all the mails why ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
suhailkaleem
Forum Newbie
Posts: 12
Joined: Thu Nov 07, 2002 9:11 am
Contact:

This code does not delete all the mails why ?

Post 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 )

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

why do you reconnect in every loop?
suhailkaleem
Forum Newbie
Posts: 12
Joined: Thu Nov 07, 2002 9:11 am
Contact:

Post 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 )

?>
seg
Forum Commoner
Posts: 38
Joined: Thu Oct 31, 2002 12:08 pm
Location: Northern, VA
Contact:

Post by seg »

Code: Select all

&lt;?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 &lt; $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); 
?&gt;
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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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

&lt;?php
$link = imap_open($mailserver, $username, $password) or die('connection failed');
$total = imap_num_msg($link);
// debugInfo
oInfo = imap_mailboxmsginfo($link);
echo 'msgs: ', oInfo-&gt;Nmsgs, '&lt;br/&gt;';
// delete-loop
$i=0;
while($i &lt; $total)
	imap_delete($link, ++$i);
//debugInfo
echo 'deleted: ', oInfo-&gt;Deleted, '&lt;br/&gt;';
echo 'errors: ';	
print_r(imap_errors());
echo '&lt;br/&gt;alerts: ';	
print_r(imap_alerts());
// close connection after expunge
imap_close($link, CL_EXPUNGE);
?&gt;
Post Reply