FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum Development » Plugins and Code Hacks » Adding References: header to mailing list emails
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Adding References: header to mailing list emails [message #168869] Wed, 31 July 2013 18:14 Go to previous message
cpreston is currently offline  cpreston   United States
Messages: 160
Registered: July 2012
Location: Oceanside
Karma:
Senior Member
This hack is designed to allow the emails your forum sends to mailing lists be properly threaded by those lists. Let me explain.

This hack is the culmination of a discussion in a how-to thread, where I learned a lot about how FUDforum handles email:
http://fudforum.org/forum/index.php?t=msg&th=118290&start=0&

Mailing lists and email clients use two headers (In-Reply-To: and References:) to help put messages in a "thread." Here's how it works. Each message is assigned a Message-ID when it is sent. This is done by your mail client (e.g. Outlook) or whatever sends the mail. In our case, this is done when FUDforum takes a new forum post and sends it to a mailing list. A Message-ID is an alphanumeric string that should be unique to that email -- yes unique in the entire world. (To accomplish this, most mail clients use a combination of the hostname the email is coming from and a string of digits that are calculated using time and some random number generators.) The following is a Message-ID that was generated by FUDforum on my test forum:

Message-ID: <58(dot)51f94326(at)test(dot)truthinit(dot)com>


When you reply to a message, such as the one with the message id above, your mail client (and FUDforum) takes the Message ID of the email you are replying to and puts it in the In-Reply-To header. And, of course, the outgoing email is assigned a new Message-ID. FUDforum also adds the X-FUDforum header. (I'm guessing FUDforum uses that for threading, and am guessing it did that before it added the feature to create the Message-ID. I just put it here for completeness.)

In-Reply-To: <58(dot)51f94326(at)test(dot)truthinit(dot)com>
X-FUDforum: 16c0e55fc10abe349c6558650b7baa51 <88>
Message-ID: <57(dot)51f942cf(at)test(dot)truthinit(dot)com>


So FUDforum is almost there. All we need to add is the References: header to give all mail clients and mailing lists what they need to thread a message. The References: header contains a list of all the Message-IDs that are a part of a thread (actually up to 10). The first Message-ID in the list is supposed to be the Message-ID of the first email in the thread. The last Message-ID should be the same as the one in the In-Reply-To header, and the (up to) eight message IDs in between those two should be the (up to) eight most recent Message-IDs prior to the latest message being replied to. This is what it would look like:

In-Reply-To: <58(dot)51f94326(at)test(dot)truthinit(dot)com>
X-FUDforum: 16c0e55fc10abe349c6558650b7baa51 <89>
References: <f(dot)51f7dbe4(at)test(dot)truthinit(dot)com> <4f(dot)51f847c8(at)test(dot)truthinit(dot)com>
	<50(dot)51f84836(at)test(dot)truthinit(dot)com> <51(dot)51f849ec(at)test(dot)truthinit(dot)com>
	<53(dot)51f84a22(at)test(dot)truthinit(dot)com> <54(dot)51f84a8f(at)test(dot)truthinit(dot)com>
	<55(dot)51f941e8(at)test(dot)truthinit(dot)com> <56(dot)51f9421f(at)test(dot)truthinit(dot)com>
	<57(dot)51f942cf(at)test(dot)truthinit(dot)com> <58(dot)51f94326(at)test(dot)truthinit(dot)com> 
Message-ID: <59(dot)51f95109(at)test(dot)truthinit(dot)com>


This hack adds that header, and is really easy to add to the current code.

1. Find mlist_post.inc in your FUDforum/includes directory.
2. Find the following section in the code:
        /* Set our own message id. */
        $tmp = parse_url($GLOBALS['WWW_ROOT']);
        $mid = dechex($msg_id) .'.'. dechex(__request_timestamp__) .'@'. $tmp['host'];
        q('UPDATE '. $GLOBALS['DBHOST_TBL_PREFIX'] .'msg SET mlist_msg_id='. _esc($mid) .' WHERE id='. $msg_id);

3. Add the following code immediately after that section:

        /*Begin References hack*/
        $references              = array();

        /*Get a list of the message ids from the current thread. The current thread is determined by asking which thread contains the id of the current post.*/
        $reference_group_result    = uq('SELECT mlist_msg_id FROM '. $GLOBALS['DBHOST_TBL_PREFIX'] .'msg WHERE thread_id=(SELECT thread_id FROM '. $GLOBALS['DBHOST_TBL_PREFIX'] .'msg WHERE id='. $msg_id .')'. $reference_thread_id . ' ORDER BY post_stamp');


        /*Create a simple array of the above results.  We need this so we can perform array_pop and array_shift operations*/
        while ($row = db_rowarr($reference_group_result)) {
                if($row[0]) $reference_msg_ids[] = $row[0];
        }

        /*First message id in this thread*/
        $first_reference_id[] = array_shift($reference_msg_ids);

        if($reference_msg_ids) {
            /*Take off the most recent message id, because it's the one from the outgoing message, not the one we're replying to*/
            array_pop($reference_msg_ids);
            /*message id of the most recently replied-to message - AKA the message id of the post immediately preceding this one*/
            $last_reference_id[]  = array_pop($reference_msg_ids);

            /*We can only have a total of 10 Message IDs in the References header.  This takes off the older ids until we get to a*/
            /*list of 8 or fewer message ids*/
            while (count($reference_msg_ids) > 8) {
                    array_shift($reference_msg_ids);
            }

        }

      /*Creates an array of message-ids to be used in the References header*/
      $references  = array_merge($first_reference_id, $reference_msg_ids, $last_reference_id);

       if($references){
             /*Starts the References header*/
             $reference_value   = 'References: ';
             while (list(, $value) = each($references)) {
                /*Appends each message-id to the References header*/
                $reference_value .= '<'.$value.'> ';
             }
       }
        $header .= $reference_value."\n";
        /*End References hack*/


Look at the header detail from the outgoing messages and you should see the References header.
[Message index]
 
Read Message
Read Message
Previous Topic: LDAP Plugin Enhancements (Updated!)
Next Topic: Email Obfuscation Plugin
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 26 10:17:29 GMT 2024

Total time taken to generate the page: 5.93859 seconds