Search code examples
phpemailimap

Getting the new ID after moving an email in 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?


Solution

  • 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.