php-imap: Exception when move a message
Hi! I’m trying to move a bunch of messages from a GMail account to a custom folder and I’m always getting an exception after 2nd or 3rd message moved.
PHP 7.3 Laravel 5.8 Webklex/laravel-imap: 2.4.0
This is the code I’m using for:
$folder = $client->getFolder('INBOX');
$messages = $this->getMessages($folder);
foreach ($messages as $message) {
$message->move($ezFolderName);
}
Exception:
Webklex \ PHPIMAP \ Exceptions \ MessageNotFoundException
message number not found
Thanks in advance!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 15 (4 by maintainers)
I ran into a similar issue. The way I solved it was by reversing the messages into an array and then moving each message.
I guess the issue has something to do with the message IDs that change after a message is moved. So if there are 3 messages (1, 2 and 3), and message 1 is moved, then 2 becomes 1, and 3 becomes 2. By the time message 3 is reached, the message with ID 3 can no longer be found. If you reverse the message order, this problem doesn’t occur, because if you move message 3 first, the IDs of messages 1 and 2 stay unchanged.
So far I have not used the
move()method itself. But when I look at the implementation, the sequence id of the message is used for moving, not the UID.UID and sequence id differ in the point that the sequence id is a kind of index of the current position of the message in the mailbox. When a message is deleted or moved, all sequence ids of the following messages change.
However, the UID always remains the same. If I understand it correctly, each time a new message is received, a counter is simply incremented and assigned to the new message. But the counter for the UID is never reset, it is always incremented by one. I could already observe this in my mailbox, where I have about 150 messages, but the last one has a UID of 1600.
It is only a guess from my side so far. But this would explain the workaround of @beilsma.
@beilsma You’re absolutely right. I ran into the same issue and your comment solved my problem! I’m collecting all messages I have to move (actually, copy and delete, because
move()in my scenario doesn’t work, but this is another matter), then I run something like$messages->reverse()->each(fn(Message $message) => $this->archiveMessage($message));and it works like a charm.
Thanks for the inspiration!