Getting unique email id with IMAP in PHP
Asked Answered
D

2

9

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?

When generating a unique id string for each email there is many other options than just the message id, fx email title and date, but I don't know what to pick: http://www.php.net/manual/en/function.imap-headerinfo.php

Diazotize answered 15/2, 2013 at 12:19 Comment(6)
Checking against (From, Date & Size) might give unique combination. Sadly very few who has worked with PHP-IMAP don't update examples in Manuals.Maronite
Does imap_uid() return message_id? What does it return for emails without message_id?Maronite
I think uid changes on some opperations.... but I am not sure...Diazotize
I think Size can change on some conditions.... Maybe I should check against (From, Date, Subject, Mailboxname, Message_id)?Diazotize
What if same sender sends multiple emails in a single second without message_id? Humans can't do it, machines(automated) can!Maronite
So you are telling that you found non-unique email strings?Centesimo
P
13

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.

Predictory answered 14/10, 2013 at 16:9 Comment(0)
O
4

according to me the unique id can be generated as follows:

key: epoch time of mail(from date field)

But at same time user can get multiple mails.

key:epoch time of mail + MailSize

At particular time a reciever id can recieve diferent mails of same size

KEY: epoch time of mail + MailSize + Recieveing Server IP (can get from recieved: field)

At particular time a reciever id can recieve diferent mails of same size from same ip also.

key: epoch time of mail + MailSize + Recieveing Server IP (can get from recieved: field) + md5sum of mail.

The possibility of duplicacy for this key is very very very low.

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.

Outpour answered 15/2, 2013 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.