Search code examples
phpgmailimap

getting the value of Message-ID from gmail imap with php


I use standard imap functions to grab mails, I need to keep track of the Message-ID (and References and In-Reply-To) to build threads. I reply to messages through the smtp, keeping the old subject, but in my web interface is not group them with others. If I add a In-Reply-To header - everything is OK.

The problem is that I can't get values of Message-ID, References, In-Reply-To (but in web interface they are present). I've tried different functions (imap_headerinfo, imap_fetchheader, imap_fetch_overview), but all of this values ​​are empty.

Please help!


Solution

  • The message ID is in a format like:

    <OTJMCQtXnqgMaP1rLJi-cD9IvuH+xuVndE-DoWAZB0cbdffqHdw@mail.gmail.com>
    

    which is parsed by the browser as an HTML tag, the following code will output a message ID in a way that can be displayed by the browser:

    $this->mbox = imap_open('{imap.gmail.com:993/imap/ssl}', $email, $password);
    $headers = imap_header($this->mbox, 1);
    echo htmlentities($headers->message_id);
    

    Or if you absolutely must use print_r:

    $this->mbox = imap_open('{imap.gmail.com:993/imap/ssl}', $email, $password);
    ob_start(); 
    print_r(imap_header($this->mbox, 1));
    print_r(imap_fetch_overview($this->mbox, 1));
    print_r(imap_fetchheader($this->mbox, 1));
    echo htmlentities(ob_get_clean());