Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the new ID after moving an email in IMAP

Tags:

php

email

imap

I am using the IMAP functions in PHP to handle my emails and import them into the database. After they are imported, I move each email to a different folder to keep things seperated.

public function moveMail($mailId, $mailBox) {
    return imap_mail_move($this->getImapStream(), $mailId, $mailBox, CP_UID) && $this->expungeDeletedMails();
}

After moving them, I determine whether or not the email is relevant. If it isn't, I want to move it to a second folder. However, after moving it, the internal mail id is changed and so I can no longer move the email.

I am looking for a way to get the id of the moved email so I can move it a second time.

Any ideas?

like image 707
Peter Avatar asked Jun 01 '16 12:06

Peter


People also ask

What is UID in IMAP?

RFC 3501 (IMAP): (Unique Identifier (UID) Message Attribute is) a 32-bit value assigned to each message, which when used with the unique identifier validity value (see below) forms a 64-bit value that MUST NOT refer to any other message in the mailbox or any subsequent mailbox with the same name forever.

What is an IMAP account?

IMAP (Internet Messaging Access Protocol) With IMAP accounts, messages are stored in a remote server. Users can log in via multiple email clients on computers or mobile device and read the same messages.


2 Answers

(This is not PHP specific and does not answer your question directly)

There are two ways this is usually done:

1) The UIDPLUS extension, if supported, should give a COPYUID response in the OK response. This includes the UIDVALIDITY and new UID in the new folder when you copy the message.

2) When this fails, opening a new connection or switching to the destination folder, and doing a UID SEARCH for the Message-ID header. If you get a single response, that's probably your message. If you get multiple responses, the highest one is probably your message (assuming multiple clients aren't copying messages with the same Message-ID at about the same time.) Since Message-ID is a sender sent header, and is not necessarily unique (messages can be duplicated), this is imperfect, but gives reasonable results most of the time. The downside is it is slow.

Luckily, nearly all servers support UIDPLUS. Unfortunately, the PHP library probably does not.

like image 113
Max Avatar answered Sep 24 '22 16:09

Max


After searching for a while I found out that there is no solution in the imap library in PHP to get the new uid. I wondered if I could just get the last uid and increment it by one but since a new email could come in that won't work either.

What might be my best solution is to use the UIDPLUS IMAP extension and consult the APPENDUID response code.

Conclusion

My final conclusion is that if the above mentioned extension is not available and your mail server does not support SEARCH HEADER, it is simply not possible to find a message by its Message-id and for that reason impossible to ever know what ID my new email got assigned.

like image 34
Peter Avatar answered Sep 22 '22 16:09

Peter