php-imap
php-imap copied to clipboard
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!
Could you resolve this issue @hernan-estalella ?
I'm having the same issue. Also when copying messages.
I have tried calling $client->reconnect() but no luck.
Hi guys... I'm having the same error. I'm using IMAP protocol to access gmail
Hi @hernan-estalella @ricard-pons @jonasschen , many thanks for the report.
Please try to change the config option options.sequence from ST_MSGN to ST_UID and try again using the message uid.
Additional information can be found here: https://www.php-imap.com/configuration/advanced
Best regards,
I am using gmail and latest stable version of Webklex/php-imap (v2.5.1). Switching from ST_MSGN to ST_UID corrects the problem if all I am doing is $message->move(). However, my emails are no longer parsed correctly when I use ST_UID and looping through multiple messages with foreach.
$message->getTextBody() gives me:
--b1_7mf1ZFeGlh7WAryz6n2DGTBIbeT0RGsIC9RUvkd5f0
Content-Type: text/plain; charset=us-ascii
This is a test email body
--b1_7mf1ZFeGlh7WAryz6n2DGTBIbeT0RGsIC9RUvkd5f0
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//"...
On the first 2 of 3 email messages in a loop. So it appears to be giving me the raw email message and not just the text body. However, it will parse the last of 3 email messages properly and just show the text email message body as it's supposed to, which is strange. I tried changing decoder and attachment options but that didn't do anything.
The parsing is not a problem when I use ST_MSGN but then $message->move() fails for the last emails after the first 2. So switching to ST_UID solves one problem but then creates another.
I ran into a similar issue. The way I solved it was by reversing the messages into an array and then moving each message.
$messages = $folder->messages()->unseen()->get();
$reverse = [];
foreach ($messages as $message) {
array_unshift($reverse, $message);
}
foreach ($reverse as $message) {
$message->move($destination);
}
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.
I ran into a similar issue. The way I solved it was by reversing the messages into an array and then moving each message.
@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!
Shouldn't the UID always be unique and unchangeable in a mailbox?
According to RFC 3501 section 2.3.1.1:
The unique identifier of a message MUST NOT change during the
session, and SHOULD NOT change between sessions. Any change of
unique identifiers between sessions MUST be detectable using the
UIDVALIDITY mechanism discussed below. Persistent unique identifiers
are required for a client to resynchronize its state from a previous
session with the server (e.g., disconnected or offline access
clients); this is discussed further in
@HelloSebastian I agree with you. Honestly, I don't know what this issue is related to... But looping over messages in reverse order did the trick. Maybe it's not about a message UID.
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.
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. [...] It is only a guess from my side so far. But this would explain the workaround of @beilsma.
Great explanation! Anyway, I think neither me nor @beilsma were talking about the Message UID.
You are absolutely right. I have read something over there. Sorry for the confusion.
You are absolutely right. I have read something over there. Sorry for the confusion.
No problem! I truly appreciated your explanation. Thanks
Still happens...
This is still happening. The workaround by @beilsma does not work consistently for me. And I don't think the issue should have been closed. We shouldn't need a workaround. We should be able to delete while cycling through messages without needing workarounds.
This is still happening. Im trying to mark messages as read, while getting unreaded messages. same error.