Getting unique email id with IMAP in PHP

2019-02-13 12:48发布

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

标签: php imap
2条回答
放荡不羁爱自由
2楼-- · 2019-02-13 13:11

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.

查看更多
女痞
3楼-- · 2019-02-13 13:12

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.

查看更多
登录 后发表回答