- fixed bugs in sorting by from and subject
[squirrelmail.git] / functions / smtp.php
index 498bc0c5f201f966021b54a67e37ffc2b8d83e1d..4c273614e558e85069bcdaf065cf4aee734667b6 100644 (file)
    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;
+      global $default_charset, $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
+      global $REMOTE_HOST;
 
       // Storing the header to make sure the header is the same
       // everytime the header is printed.
          $cc_list = getLineOfAddrs($cc);
          $bcc_list = getLineOfAddrs($bcc);
          
+         /* Encoding 8-bit characters and making from line */
+         $subject = encodeHeader($subject);
          if ($from == "")
             $from = "<$from_addr>";
          else
-            $from = $from . " <$from_addr>";
-
-         /* Encoding 8-bit characters */
-         $subject = encodeHeader($subject);
-         $from = encodeHeader($from);
+            $from = encodeHeader($from) . " <$from_addr>";
          
          /* This creates an RFC 822 date */
          $date = date("D, j M Y H:i:s ", mktime()) . timezone();
 
          /* Create a message-id */
          $message_id = "<" . $REMOTE_PORT . "." . $REMOTE_ADDR . ".";
-         $message_id .= time() . "@" . $SERVER_NAME .">";
+         $message_id .= time() . ".squirrel@" . $SERVER_NAME .">";
          
          /* Make an RFC822 Received: line */
-         $header = "Received: from $REMOTE_ADDR by $SERVER_NAME with HTTP; ";
-         $header .= "$date\r\n";
+         if (isset($REMOTE_HOST))
+            $received_from = "$REMOTE_HOST ([$REMOTE_ADDR])";
+         else
+            $received_from = $REMOTE_ADDR;
+    
+         if (isset($HTTP_VIA) || isset ($HTTP_X_FORWARDED_FOR)) {
+            if ($HTTP_X_FORWARDED_FOR == "")
+               $HTTP_X_FORWARDED_FOR = "unknown";
+            $received_from .= " (proxying for $HTTP_X_FORWARDED_FOR)";
+         }            
+    
+         $header  = "Received: from $received_from\r\n";
+         $header .= "        (SquirrelMail authenticated user $username)\r\n";
+         $header .= "        by $SERVER_NAME with HTTP;\r\n";
+         $header .= "        $date\r\n";
          
          /* Insert the rest of the header fields */
          $header .= "Message-ID: $message_id\r\n";
          writeBody ($imap_stream, $body); 
          sqimap_append_done ($imap_stream);
       }   
-
+      sqimap_logout($imap_stream); 
       // Delete the files uploaded for attaching (if any).
       deleteAttachments();
    }