remove unnessecarry \r\n
[squirrelmail.git] / class / deliver / Deliver.class.php
index d60757fb546c2416c29552fed5702fe15f9bc88c..2dfb2c71880d8b1d57fcabb4c9d3df879695f964 100644 (file)
@@ -22,10 +22,11 @@ class Deliver {
        } else {
           $boundary='';
        }
+       $raw_length = 0;
        $reply_rfc822_header = (isset($message->reply_rfc822_header) 
                             ? $message->reply_rfc822_header : '');
-       $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header);
-       $raw_length = strlen($header);
+       $header = $this->prepareRFC822_Header($rfc822_header, $reply_rfc822_header, $raw_length);
+
        if ($stream) {
             $this->preWriteToStream($header);
             $this->writeToStream($stream, $header);
@@ -55,7 +56,7 @@ class Deliver {
            if ($i == $entCount-1) $last = true;
        }
         if ($boundary && $last) {
-           $s = "\r\n--".$boundary."--\r\n\r\n";
+           $s = "--".$boundary."--\r\n\r\n";
            $length_raw += strlen($s);
            if ($stream) {
                 $this->preWriteToStream($s);
@@ -70,6 +71,8 @@ class Deliver {
        } else {
           $type0 = $message->rfc822_header->content_type->type0;
        }
+       
+       $body_part_trailing = $last = '';
        switch ($type0) {
        case 'text':
        case 'message':
@@ -80,15 +83,17 @@ class Deliver {
                   $this->preWriteToStream($body_part);     
                  $this->writeToStream($stream, $body_part);
               }
+              $last = $body_part;
            } elseif ($message->att_local_name) {
                $filename = $message->att_local_name;
                $file = fopen ($filename, 'rb');
-               while ($tmp = fgets($file, 4096)) {
-                  $length += $this->clean_crlf($tmp);
+               while ($body_part = fgets($file, 4096)) {
+                  $length += $this->clean_crlf($body_part);
                   if ($stream) {
-                     $this->preWriteToStream($tmp);
-                     $this->writeToStream($stream, $tmp);
+                     $this->preWriteToStream($body_part);
+                     $this->writeToStream($stream, $body_part);
                    }
+                  $last = $body_part;
                }
                fclose($file);
             }
@@ -103,22 +108,28 @@ class Deliver {
            } elseif ($message->att_local_name) {
                $filename = $message->att_local_name;
                $file = fopen ($filename, 'rb');
+               $encoded = '';
                while ($tmp = fread($file, 570)) {
-                  $encoded = chunk_split(base64_encode($tmp));
-                  
+                  $body_part = chunk_split(base64_encode($tmp));
+                  $length += $this->clean_crlf($body_part);
                   if ($stream) {
-                     $this->writeToStream($stream, $encoded);
+                     $this->writeToStream($stream, $body_part);
                   }
                }
                fclose($file);
            }
            break;
        }
-       $body_part_trailing = "\r\n";
-       $length += strlen($body_part_trailing);
-       if ($stream) {
-           $this->preWriteToStream($body_part_trailing);     
-          $this->writeToStream($stream, $body_part_trailing);
+       $body_part_trailing = '';
+       if ($last && substr($last,-1) != "\n") {
+          $body_part_trailing = "\r\n";
+       }
+       if ($body_part_trailing) {
+           $length += strlen($body_part_trailing);
+           if ($stream) {
+               $this->preWriteToStream($body_part_trailing);     
+               $this->writeToStream($stream, $body_part_trailing);
+           }
        }
     }
     
@@ -201,14 +212,14 @@ class Deliver {
        $cnt = count($header);
        $hdr_s = '';
        for ($i = 0 ; $i < $cnt ; $i++) {
-           $hdr_s .= $this->foldLine($header[$i], 78, '    ');
+           $hdr_s .= $this->foldLine($header[$i], 78,str_pad('',4));
        }
        $header = $hdr_s;
        $header .= $rn; /* One blank line to separate mimeheader and body-entity */
        return $header;
     }    
 
-    function prepareRFC822_Header($rfc822_header, $reply_rfc822_header) {
+    function prepareRFC822_Header($rfc822_header, $reply_rfc822_header, &$raw_length) {
        global $REMOTE_ADDR, $SERVER_NAME, $REMOTE_PORT;
        global $version, $useSendmail, $username;
        global $HTTP_VIA, $HTTP_X_FORWARDED_FOR;
@@ -250,7 +261,9 @@ class Deliver {
         if (count($rfc822_header->from) > 1) {
            $header[] = 'Sender: '. encodeHeader($rfc822_header->getAddr_s('sender')) . $rn;
        }
-       $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;    // Who it's TO
+       if (count($rfc822_header->to)) {
+           $header[] = 'To: '. encodeHeader($rfc822_header->getAddr_s('to')) . $rn;
+        }
        if (count($rfc822_header->cc)) {
            $header[] = 'Cc: '. encodeHeader($rfc822_header->getAddr_s('cc')) . $rn;
        }
@@ -259,8 +272,14 @@ class Deliver {
        }
        /* Sendmail should return true. Default = false */
        $bcc = $this->getBcc();
-       if ($bcc && count($rfc822_header->bcc)) {
-           $header[] = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
+       if (count($rfc822_header->bcc)) {
+           $s = 'Bcc: '. encodeHeader($rfc822_header->getAddr_s('bcc')) . $rn;
+           if (!$bcc) {
+              $s = $this->foldLine($s, 78, str_pad('',4));
+              $raw_length += strlen($s);
+           } else {
+              $header[] = $s;
+           }
        }
        /* Identify SquirrelMail */     
        $header[] = "X-Mailer: SquirrelMail (version $version)" . $rn; 
@@ -303,10 +322,11 @@ class Deliver {
        $cnt = count($header);
        $hdr_s = '';
        for ($i = 0 ; $i < $cnt ; $i++) {
-           $hdr_s .= $this->foldLine($header[$i], 78, '    ');
+           $hdr_s .= $this->foldLine($header[$i], 78, str_pad('',4));
        }
        $header = $hdr_s;
        $header .= $rn; /* One blank line to separate header and body */
+       $raw_length += strlen($header);
        return $header;
     }