Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an email thread from IMAP?

Tags:

email

imap

I would like to create a "thread view" from emails that are on an IMAP server.

To achieve that, I fetch the list of emails in the INBOX and other folders but I need to know which email is answering which. Is there such a link between emails in IMAP?

For example on an IMAP server each email has a unique ID: if email B is an answer to email A, is the ID of A stored inside email B?

like image 522
Matthieu Napoli Avatar asked Feb 26 '15 10:02

Matthieu Napoli


2 Answers

If your IMAP server supports the "thread" capability as described in RFC 5256, you can just ask the server to thread the messages for you.

Otherwise, you'll have to fetch the relevant information and do the threading on the client. The RFC describes two algorithms to do that. The simpler one, ORDEREDSUBJECT, just groups messages by subject and then sorts them by date. This gives a flat threading structure. The more complicated one, REFERENCES, looks at the In-Reply-To and References headers of each message, and considers messages with such headers to be children of the message with the given Message-Id.

like image 51
legoscia Avatar answered Nov 13 '22 22:11

legoscia


The classic way is to retrieve the message-id and references fields. If two messages contain the same message-id in either message-id or references, then they are in the same thread.

Gmail has a new and IMO better way: each thread has a numeric ID which you can retrieve using x-gm-thrid. Google has published example code in various languages for using that (there should be links near that code).

like image 37
arnt Avatar answered Nov 13 '22 22:11

arnt