I'm trying to move messages away from Inbox into Processed label with this code:
$inbox = imap_open($host,$user,$pass) or die('Error: ' . imap_last_error());
if( $emails = imap_search($inbox,'ALL') )
{
foreach($emails as $email_number) {
imap_mail_move($inbox, $email_number, 'Processed') or die('Error');
}
}
imap_expunge($inbox);
imap_close($inbox);
Unfortunately, while the messages get the Processed label, they're still left in Inbox too.
How would I make them go away from Inbox?
Actually... The reason why the emails were left in the inbox was that when imap_mail_move did it's thing, the IDs of all the leftover messages got decremented by one, so when the foreach loop moved to the next message, one message was left behind. This skipping a message repeated for every iteration. That's why it seemed that imap_mail_move was not working.
The solution is to use unique message UIDs instead of potentially repeating IDs:
$inbox = imap_open( $host, $user, $pass );
$emails = imap_search( $inbox, 'ALL', SE_UID );
if( $emails ) {
foreach( $emails as $email_uid ) {
imap_mail_move($inbox, $email_uid, 'processed', CP_UID);
}
}