Fetch email using message id with php imap?

Aditya picture Aditya · Jan 14, 2014 · Viewed 7.1k times · Source

I am developing a small webmail application, what i need to do is thread the emails like what Gmail does .

I planned on achieving it by getting 'references' of a mail (using uid) and then showing them as one thread. I get the references like this:

    $inbox = imap_open("{imap.example.org:143}INBOX", "username", "password");
    $email_number = imap_msgno($inbox,$uid);
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $mess = $overview[0];
    $refs = array_filter(explode(' ', htmlentities($mess->references)));

The $refs array is an array of Message-Id's , can anyone tell me how to fetch a mail based on a Message-Id .

If i can get a Message UID or Message Sequence Number from a Message-Id that also would suffice.

An alternative that came up in my mind was to achieve this is by using imap_search() for searching mails with same subject(after stripping 'Re:' from it etc) but i don't think it would be ideal.

Can anyone give me helpful pointers as to how to solve this? Thanks in advance

Answer

Max picture Max · Jan 15, 2014

Some IMAP servers will allow you to search by Message-ID (SEARCH HEADER Message-ID string), but a lot of server software seems to implement this poorly.

In general, there is no way to fetch a message by its Message-ID header. Most clients download the headers (including the Message-ID) of all messages, store them, and then post-process them, matching them up with other messagesbased on References and In-Reply-To headers. However: If you're using Gmail, you can use its extensions to grab Gmail's internal thread-id, which they call X-GM-THRID.