瀏覽代碼

Insert In-Reply-To and References headers when replying to a message.

pallo 25 年之前
父節點
當前提交
7929a509a2
共有 3 個文件被更改,包括 51 次插入23 次删除
  1. 21 13
      functions/imap_messages.php
  2. 1 1
      functions/mime.php
  3. 29 9
      functions/smtp.php

+ 21 - 13
functions/imap_messages.php

@@ -40,28 +40,33 @@
    /******************************************************************************
     **  Returns some general header information -- FROM, DATE, and SUBJECT
     ******************************************************************************/
-	class small_header {
-		var $from, $subject, $date, $to, $priority;
-	}
+   class small_header {
+      var $from, $subject, $date, $to, $priority, $message_id;
+   }
 	 
    function sqimap_get_small_header ($imap_stream, $id, $sent) {
-      fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Subject X-Priority)]\r\n");
+      fputs ($imap_stream, "a001 FETCH $id BODY.PEEK[HEADER.FIELDS (Date To From Subject Message-Id X-Priority)]\r\n");
       $read = sqimap_read_data ($imap_stream, "a001", true, $response, $message);
 
       $subject = _("(no subject)");
       $from = _("Unknown Sender");
       $priority = "0";
-		$g = 0;
+      $messageid = "<>";
+
+      $g = 0;
       for ($i = 0; $i < count($read); $i++) {
          if (eregi ("^to:", $read[$i])) {
             $to = sqimap_find_displayable_name(substr($read[$i], 3));
-			}	
+	 }	
          if (eregi ("^from:", $read[$i])) {
             $from = sqimap_find_displayable_name(substr($read[$i], 5));
-			}	
+	 }	
          if (eregi ("^x-priority:", $read[$i])) {
             $priority = trim(substr($read[$i], 11));
          }
+         if (eregi ("^message-id:", $read[$i])) {
+            $messageid = trim(substr($read[$i], 11));
+         }
          if (eregi ("^date:", $read[$i])) {
             $date = substr($read[$i], 5);
          } else if (eregi ("^subject:", $read[$i])) {
@@ -69,18 +74,21 @@
             if (trim($subject) == "")
                $subject = _("(no subject)");
          }
-		}	
+      }
 
-		$header = new small_header;
+      $header = new small_header;
       if ($sent == true)
          $header->from = $to;
       else   
-		   $header->from = $from;
-		$header->date = $date;
-		$header->subject = $subject;
+	 $header->from = $from;
+
+      $header->date = $date;
+      $header->subject = $subject;
       $header->to = $to;
       $header->priority = $priority;
-		return $header;
+      $header->message_id = $messageid;
+
+      return $header;
    }
 
    /******************************************************************************

+ 1 - 1
functions/mime.php

@@ -26,7 +26,7 @@
       var $type0, $type1, $boundary, $charset, $encoding;
       var $to, $from, $date, $cc, $bcc, $reply_to, $subject;
       var $id, $mailbox, $description;
-      var $entity_id;
+      var $entity_id, $message_id;
    }
    
    class message {

+ 29 - 9
functions/smtp.php

@@ -106,7 +106,7 @@
    }
 
    /* Print all the needed RFC822 headers */
-   function write822Header ($fp, $t, $c, $b, $subject) {
+   function write822Header ($fp, $t, $c, $b, $subject, $more_headers) {
       global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
       global $data_dir, $username, $domain, $version, $useSendmail;
       global $default_charset;
@@ -156,7 +156,15 @@
          $header .= "Subject: $subject\r\n";
          $header .= "From: $from\r\n";
          $header .= "To: $to_list \r\n";    // Who it's TO
-         
+
+	 /* Insert headers from the $more_headers array */
+	 if(is_array($more_headers)) {
+	    reset($more_headers);
+	    while(list($h_name, $h_val) = each($more_headers)) {
+	       $header .= sprintf("%s: %s\r\n", $h_name, $h_val);
+	    }
+	 }
+
          if ($cc_list) {
             $header .= "Cc: $cc_list\r\n"; // Who the CCs are
          }
@@ -231,13 +239,13 @@
    }
 
    // Send mail using the sendmail command
-   function sendSendmail($t, $c, $b, $subject, $body) {
+   function sendSendmail($t, $c, $b, $subject, $body, $more_headers) {
       global $sendmail_path, $username, $domain;
 
       // open pipe to sendmail
       $fp = popen (escapeshellcmd("$sendmail_path -t -f$username@$domain"), "w");
       
-      $headerlength = write822Header ($fp, $t, $c, $b, $subject);
+      $headerlength = write822Header ($fp, $t, $c, $b, $subject, $more_headers);
       $bodylength = writeBody($fp, $body);
 
       pclose($fp);
@@ -256,7 +264,7 @@
       }
    }
 
-   function sendSMTP($t, $c, $b, $subject, $body) {
+   function sendSMTP($t, $c, $b, $subject, $body, $more_headers) {
       global $username, $domain, $version, $smtpServerAddress, $smtpPort,
          $data_dir, $color;
 
@@ -313,7 +321,7 @@
       errorCheck($tmp);
 
       // Send the message
-      $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject);
+      $headerlength = write822Header ($smtpConnection, $t, $c, $b, $subject, $more_headers);
       $bodylength = writeBody($smtpConnection, $body);
 
       fputs($smtpConnection, ".\r\n"); // end the DATA part
@@ -430,22 +438,34 @@
    function sendMessage($t, $c, $b, $subject, $body, $reply_id) {
       global $useSendmail, $msg_id, $is_reply, $mailbox;
       global $data_dir, $username, $domain, $key, $version, $sent_folder, $imapServerAddress, $imapPort;
+      $more_headers = Array();
 
       $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
+
       if ($reply_id) {
          sqimap_mailbox_select ($imap_stream, $mailbox);
          sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, "Answered");
+
+	 // Insert In-Reply-To and References headers if the 
+	 // message-id of the message we reply to is set (longer than "<>")
+	 // The References header should really be the old Referenced header
+	 // with the message ID appended, but it can be only the message ID too.
+	 $hdr = sqimap_get_small_header ($imap_stream, $reply_id, false);
+	 if(strlen($hdr->message_id) > 2) {
+	    $more_headers["In-Reply-To"] = $hdr->message_id;
+	    $more_headers["References"]  = $hdr->message_id;
+	 }
       }
       
       if ($useSendmail==true) {  
-         $length = sendSendmail($t, $c, $b, $subject, $body);
+         $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers);
       } else {
-         $length = sendSMTP($t, $c, $b, $subject, $body);
+         $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers);
       }
 
       if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
          sqimap_append ($imap_stream, $sent_folder, $length);
-         write822Header ($imap_stream, $t, $c, $b, $subject);
+         write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers);
          writeBody ($imap_stream, $body); 
          sqimap_append_done ($imap_stream);
       }