6

I am saving my mailbox elements to a mysql database (to perform fast searches in my intranet, since imap_search' is too slow).

I am connecting to the server and folder, and iterating through messages.

simplified code:

$numMsg = imap_num_msg($conn);

for($i=1;$i<=$numMsg;$i++){
    $uid = imap_uid($conn,$i);
    echo("msg_num:".$i." - uid:".$uid);
}

and I get something like this:

msg_num:5 - uid:5msg_num:6 - uid:6msg_num:7 - uid:7msg_num:8 - uid:8msg_num:9 - uid:9msg_num:10 - uid:10msg_num:11 - uid:11msg_num:12 - uid:12

which is totally wrong!!!

uid isn't supposed to be unique?

I get this UIDs in 5 sub-folders that I have and also in Sent Items, on the Inbox I get uids right (msg_num:5 - uid:1503msg_num:6 - uid:1504msg_num:7 - uid:1506)

user1978142
  • 7,946
  • 3
  • 17
  • 20
Alvaro Hernandorena
  • 610
  • 1
  • 5
  • 18
  • This might shed some light: http://stackoverflow.com/questions/14894624/getting-unique-email-id-with-imap-in-php – Matt Gibson Apr 25 '14 at 17:22

1 Answers1

17

Right, the UID is only unique per folder. The full persistent unique ID of a message is a tuple of the folder name, the folders UIDVALIDITY, and the messages UID. That tuple, on a correctly implemented server, will only ever refer to one message.

For example: (SENT, 1, 100) Indicates message with ID 100 from the 1st incarnation of the sent folder. UIDVALIDITYs tend to be about 10 digit numbers, and are supposed to change if the folder is deleted and recreated or needs to be reindexed/regenerated by the server software.

Max
  • 10,701
  • 2
  • 24
  • 48
  • thank you Max, I thought that UID was unique in all mailbox, I was using UID to see if the email was already in the mysql DB, but now I will check with other values, now checking if the email is in the DB comparing DATE, SUBJECT, FROM address and TO address instead of just UID. – Alvaro Hernandorena Apr 25 '14 at 19:02
  • 1
    That is still dangerous. You should include the folder name and UID validity as your extra keys. Then it will be fully unique. – Max Apr 25 '14 at 19:03
  • I am not sure about using folder name, since the email could change folder and still be the same email, or not? by uid you mean the regular uid? or UID validity is other thing? – Alvaro Hernandorena Apr 25 '14 at 19:06
  • 3
    Unfortunately, the IMAP protocol does not allow you to normally track emails across folders. When it moves folders, it becomes an entirely new message. It doesn't keep its UID. In fact, the baseline IMAP specification doesn't even have a MOVE command. You have to COPY it to another folder (which of course must be a new message), and then delete the first one. – Max Apr 25 '14 at 19:17
  • A reasonable design is to have a Folder table, tracking folder names and their UIDVALIDITYs, with an auto generated primary key. Then associate each message with one of these folders. If you later connect and discover the folder no longer exists or its UIDVALIDITY has changed, you delete your local copy of any cached messages. But now each message is identified by a folder_id and its UID. – Max Apr 25 '14 at 19:19
  • Ic Max... ur right about that. So going back a little, why u think is dangerous to base on date, from,to and subject? I would be very strange to get an email from same person with same subject at the exact same time, my date field uses this format (2013-08-20 15:00:52) – Alvaro Hernandorena Apr 25 '14 at 19:23
  • Messages can get duplicated due to copying, SMTP hiccups, etc. – Max Apr 25 '14 at 19:36