Reputation:
There is a strange behaviour I am noticing with PHP imap_mail_move
function. Suppose there are two mails in the inbox folder and I want to move it to a different folder on by one, I am facing some trouble (atleast in gmail).
If I am using
imap_mail_move( $imap_connection, 1, 'ARCHIVE' );
imap_mail_move( $imap_connection, 2, 'ARCHIVE' );
Message 1 tag change from INBOX
to IMAP/ARCHIVE
Message 2 tag change from INBOX
to IMAP/ARCHIVE
INBOX
(both) rather than just IMAP/ARCHIVE
I am unsure why it is happening. Why it is not working as expected second time.
Note: It works properly if I use this code.
imap_mail_move( $imap_connection, 2, 'ARCHIVE' );
imap_mail_move( $imap_connection, 1, 'ARCHIVE' );
or
imap_mail_move( $imap_connection, 1, 'ARCHIVE' );
$imap_connection = imap_open( $mailbox, $connection_details['username'], $connection_details['password'] );
imap_mail_move( $imap_connection, 2, 'ARCHIVE' );
Upvotes: 2
Views: 3225
Reputation: 96
IMAP base spec has 2 different IDs --Unique IDs (UID) and sequence numbers (seqnum).
UID will never change unless the UIDVALIDITY changes, and that's not something that happens mid-session. In contrast, seqnum identifies the ordinal position of the message in the folder; 1 is the first message, 2 is the 2nd, etc. As a result, moving/deleting a message causes all more recent messages to have their seqnum updated.
Moving by UID is probably the safest way to go, but moving from highest seqnum to lowest seqnum should also be OK.
Upvotes: 0
Reputation: 5043
every time you call imap_mail_move it will change the underlying message numbers
imap_mail_move($mbox, $msgno, '[Gmail]/Trash');
solution
imap_mail_move($mbox, '1,2', '[Gmail]/Trash');
imap_close($connection, CL_EXPUNGE);
Upvotes: 0
Reputation: 1192
Can you please try use "UID" in moving function instead of message number? For me this work as expected.
$connection = imap_open($mailConnectionLine, $mailUser, $mailPassword);
// get emails
$emailsInbox = imap_search($connection, 'ALL', SE_UID);
foreach ($emailsInbox as $emailUID) {
// Move
$movingResult = imap_mail_move($connection, $emailUID, $destinationFolder, CP_UID);
}
imap_close($connection, CL_EXPUNGE);
Upvotes: 1