PHP Imap: Threaded Email Conversations

3.6k views Asked by At

I'm new to IMAP functions in PHP and I am tasked to build some ticketing site.
I get the basic email fetching part but I kinda stumbled on the "Threaded Conversation View" hurdle.

I need to be able to present the emails (both sent and received) in a threaded conversation view much like a smartphone's SMS facility.

Most of the algorithms I found all just dealt with threaded inbox excluding the sent items. It would be nice if ya'll can help me with this.

My final target result would be, initially, an array of "UNREAD" mails grouped per subject each containing a trail of exchanged emails belonging to that subject.

Something like this:

array
(
  [0] => array
         (
           [0] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'yes i am',
                    'subject' => 'Re: Fubar',
                    'status' => 'unread'
                  ),
           [1] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'are you from america?',
                    'subject' => 'Re: Fubar',
                    'status' => 'read'
           [2] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'hello',
                    'subject' => 'Re: Fubar',
                    'status' => 'read'
           [3] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'hi',
                    'subject' => 'Fubar',
                    'status' => 'read'
         ),
  [1] => array
         (
           [0] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'hell yeah!',
                    'subject' => 'Re: Skills',
                    'status' => 'unread'
                  ),
           [1] => array
                  (
                    'date' => 'some date',
                    'sender' => 'some sender',
                    'message' => 'are you good enough?',
                    'subject' => 'Skills',
                    'status' => 'read'
)
1

There are 1 answers

0
Travis Hegner On

Perhaps imap_thread() is what you are after?

You'd have to loop through the results, and build your example array using imap_headerinfo(). As long as the mailbox you are querying against is kept trim, it ought to run fairly quickly.