How can I calculate a unique id string for each email in an IMAP account?

I am making a script which must frequently copy all missing mails from one IMAP account to another one. I want to avoid making duplicates on each update, so I have to identify what is on one account and what is on the other.

Not all emails have a message_id, and I can't see what the difference between message_id and uid is - can anybody tell me?

It seems to me that the message_id is not changed when using imap_append - can anybody confirm that?

2 Answers
2

UID is unique within a mailbox, but does not map between mailboxes, so is of no use for matching emails between mailboxes.

message_id is intended to be globally unique for all emails and is generated by the sending email server. If the server is configured correctly, every message it sends will have a message_id, and this can be used to match an email across mailboxes. However, badly configured servers may not assign a message_id. In this case, a hash of senderaddress & udate has always proved to be unique for me - if the emails came from the same person at the same microsecond, it's gonna be the same message. Note - use sender rather than from - from can be spoofed easier than sender.

Message id is usually an identifier for the device that sent the message or may be something else, totally depends on domain and can be same for different mails and possibly might not exists altogether.

uid is something by which imap server tracks the mails identity. but if in between the mail has been deleted or moved and coz of buggy server code, its possible that a different mail might get assigned with same uid.