Creating an email thread from IMAP?
Asked Answered
M

2

5

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?

Moleskin answered 26/2, 2015 at 10:14 Comment(0)
R
9

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.

Rapt answered 26/2, 2015 at 11:50 Comment(1)
Guess I'll have to look at In-Reply-To and References :) ThanksMoleskin
W
6

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).

Weintraub answered 26/2, 2015 at 12:13 Comment(4)
Also useful is the In-Reply-To header. With Message-ID, In-Reply-To and References, you can build a tree view of which messages are related to which messages. Both headers are useful as not all clients set both.Vegetation
IMO: was useful, but not so much any more. There are many clients that send References, some that send nothing, some that send unparseable In-Reply-To, but the clients that sent machine-readable I-R-T and not References seem to be heading for extinction. Do you have a different impression?Weintraub
Code I work on has to be very broadly compatible, so if it it's a parsable message-id in i-r-t, it's added for consideration for threading information. We generate both just in case.Vegetation
Mind you don't classify <[email protected]> as a message-id then ;)Weintraub

© 2022 - 2024 — McMap. All rights reserved.