Fetch email using message id with php imap?
Asked Answered
P

2

5

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

Plunge answered 14/1, 2014 at 21:40 Comment(0)
N
7

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.

Ninon answered 15/1, 2014 at 0:43 Comment(2)
Thanks Max ,but is there a way to search header with php , like with imap_search or any other function where i can do SEARCH HEADER Message-ID string?Plunge
Sorry, I know IMAP, not PHP, so I'm giving general information that should be translatable into any given library, if it's extensible enough. imap_search is where I'd start, yes.Ninon
N
2

This worked for me:

$result = imap_search($imapResource, "TEXT \"<[email protected]>\"", SE_UID);
if(is_array($result) && count($result) == 1){
    echo $result[0];
}
Nevsa answered 24/9, 2022 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.