Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message getting lost during iteration of mailbox if message moved #34

Open
cjwnz opened this issue Nov 24, 2022 · 5 comments
Open

Message getting lost during iteration of mailbox if message moved #34

cjwnz opened this issue Nov 24, 2022 · 5 comments

Comments

@cjwnz
Copy link

cjwnz commented Nov 24, 2022

Please see example code below. I have found that I'm losing messages in the loop if the message is moved to a subfolder during processing.
For example, let's say I have 3 messages to process.
After message 1 is processed and moved to a subfolder then message 3 becomes the second message to be processed and message 2 is lost. This used to work fine before moving to php-imap2 functions.
After processing message 2 will still be in the INBOX folder - its just not being picked up in the loop.

// get number of messages
$count = imap2_num_msg($mbox);

for ($msgno = 1; $msgno  <= $count; $msgno++)  {
  
  // get header
  $header = imap2_headerinfo($mbox, $msgno);

  // process message
  process_mesage($mbox,$msgno);

  // move message to subfolder
  imap2_mail_move($mbox, $msgno, "INBOX.PROCESSED");

}
@cjwnz
Copy link
Author

cjwnz commented Nov 24, 2022

** UPDATE **

If I use imap2_mail_copy() followed by imap2_delete() INSTEAD of imap2_mail_move() it works as expected.
So the bug must be in imap2_mail_move() function.

Cheers

@pjio
Copy link

pjio commented Nov 28, 2022

Have you tried to move the messages in reverse? (for ($msgno = $count; $msgno >= 1; $msgno--) { ... }.
After move or copy + delete + expunge the numbers of the mails change (I assume, copy + delete without expunge works because the number is unchanged until expunge.

src/Roundcube/ImapClient.php in line 2428 calls expunge which likely causes the different behaviour from imap_mail_move()

@cjwnz
Copy link
Author

cjwnz commented Nov 28, 2022

great - well spotted. so for now I have a workaround but will it be fixed in new release?

@IZSkiSurfer
Copy link

Afaik this is not really a bug rather a lucky surprise that it works with the imap_ lib.
You are working here with numbers rather than IDs. According to the official PHP doc the mail numbers may change with every imap command.

Instead of using imap(2)_headerinfo which only works with msgno you should use e.g. $hdr = imap2_rfc822_parse_headers(imap2_fetchheader()) and work with message IDs as those don't change.

To get a list of all UIDs you could use e.g. imap2_fetch_overview

@IZSkiSurfer
Copy link

Have you tried to move the messages in reverse? (for ($msgno = $count; $msgno >= 1; $msgno--) { ... }. After move or copy + delete + expunge the numbers of the mails change (I assume, copy + delete without expunge works because the number is unchanged until expunge.

src/Roundcube/ImapClient.php in line 2428 calls expunge which likely causes the different behaviour from imap_mail_move()

I guess you could simply replace the client->move call in Mail class by client->copy and add the expunge flag afterwards..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants