Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting unique email id with IMAP in PHP

Tags:

php

imap

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

like image 569
Kasper DK Avatar asked Feb 15 '13 12:02

Kasper DK


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 UID in PHP?

An UID is a unique identifier that will not change over time while a message sequence number may change whenever the content of the mailbox changes. This function is the inverse of imap_msgno().

Whats an IMAP server?

IMAP allows you to access your email wherever you are, from any device. When you read an email message using IMAP, you aren't actually downloading or storing it on your computer; instead, you're reading it from the email service.


2 Answers

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.

like image 193
unarmed wombat Avatar answered Oct 12 '22 20:10

unarmed wombat


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.

like image 27
Anshul Avatar answered Oct 12 '22 20:10

Anshul