John Davidson

imap - PHP imap_msgno returns zero once message moved from inbox, no way to move messages a second time

0 comments
Message:


In order to move a message using PHP's imap_mail_move() function you need the not-an-id fluff "id" that can be determined by using PHP's imap_msgno() function.


I have found across all the mail providers that once any given message is moved out of the inbox that imap_msgno() will always return 0. The documentation page is bare bones is of absolutely no help.


I've filed a PHP bug though I'm hoping someone has some sort of insight. I've tested the latest versions of PHP 7.3, 8.0 and 8.1 as of December 5th, 2021.


How do I force PHP's imap_msgno() to return the message id instead of zero once a message has been moved from the inbox?



  • I have tested AOL, Gmail and Yahoo all with the same results across all the versions of PHP I've tested.

  • I have been able to move the messages from the inbox to other folders via PHP's imap_mail_move() function.

  • Yes, I'm using PHP's imap_expunge() function to commit the changes.

  • Yes, I'm obviously passing the actual correct unique id.

  • Yes, I've verified that the messages were moved by inspecting the folders directly at the mentioned mail providers.

  • Yes, for the sake of trying everything I can think of I've attempted to move the messages using the unique id (in spite of the documentation for imap_mail_move() stating it requires the fluff "id") and obviously the attempt failed.


The errors for imap_mail_move() are obviously caused by not having the valid fluff "id" generated by imap_msgno(); I'm posting them here in case others are encountering this issue and haven't realized the root cause.



Unknown: IMAP protocol error: [CLIENTBUG] COPY Command arguments invalid (errflg=2)




Unknown: [CLIENTBUG] COPY Command arguments invalid (errflg=2)




My Answer:
The issue you are facing with the imap_msgno returning zero after moving a message from the inbox is a common problem when working with the PHP IMAP functions.

To successfully move a message using the imap_mail_move() function, you need to use the unique message identifier (UID) instead of the message number. The UID is a unique identifier assigned to each message in the mailbox and remains the same even if the message is moved to a different folder.

You can retrieve the UID of a message using the imap_uid() function before moving it. Here is an example code snippet to demonstrate how to move a message using the UID:

php
$inbox = imap_open('{mail.example.com:993/imap/ssl}INBOX', 'username', 'password');

// Get the UID of the message
$uid = imap_uid($inbox, $msgno);

// Move the message to a different folder
imap_mail_move($inbox, $uid, 'INBOX.Archive');

imap_expunge($inbox);
imap_close($inbox);


By using the UID instead of the message number, you can successfully move messages multiple times without any issues. Make sure to replace 'mail.example.com', 'username', 'password', and 'INBOX.Archive' with your actual server details and folder names.

Rate this post

4 of 5 based on 4989 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us