parsing $message to magicHTML for better handling message/rfc822 attachments
[squirrelmail.git] / functions / smtp.php
index 24f92d03053a1496450e5bdf27474a508dc092cf..d10f6854a45a541b0b6d7b49a86bbe59c395be85 100644 (file)
@@ -147,8 +147,10 @@ function attachFiles ($fp, $session, $rn="\r\n") {
                 $file = fopen ($filename, 'rb');
                 if (substr($filetype, 0, 5) == 'text/' ||
                     substr($filetype, 0, 8) == 'message/' ) {
-                    $header .= "$rn";
-                    fputs ($fp, $header);
+                    $header .= $rn;
+                                       if ($fp) {
+                                           fputs ($fp, $header);
+                                       }
                     $length += strlen($header);
                     while ($tmp = fgets($file, 4096)) {
                         $tmp = str_replace("\r\n", "\n", $tmp);
@@ -160,21 +162,23 @@ function attachFiles ($fp, $session, $rn="\r\n") {
                          * Check if the last line has newline ($rn) in it
                          * and append if it doesn't.
                          */
-                        if (feof($fp) && !strstr($tmp, "$rn")){
-                            $tmp .= "$rn";
+                        if ($fp && feof($fp) && !strstr($tmp, "$rn")){
+                            $tmp .= $rn;
+                        }
+                        if ($fp) {
+                            fputs($fp, $tmp);
                         }
-                        fputs($fp, $tmp);
                         $length += strlen($tmp);
                     }
                 } else {
                     $header .= "Content-Transfer-Encoding: base64" 
                         . "$rn" . "$rn";
-                    fputs ($fp, $header);
+                    if ($fp) fputs ($fp, $header);
                     $length += strlen($header);
                     while ($tmp = fread($file, 570)) {
                         $encoded = chunk_split(base64_encode($tmp));
                         $length += strlen($encoded);
-                        fputs ($fp, $encoded);
+                        if ($fp) fputs ($fp, $encoded);
                     }
                 }
                 fclose ($file);
@@ -254,25 +258,21 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn
     /* Storing the header to make sure the header is the same
      * everytime the header is printed.
      */
-    static $header, $headerlength;
+    static $header, $headerlength, $headerrn;
     
     if ($header == '') {
+               $headerrn = $rn;
         $to = expandAddrs(parseAddrs($t));
         $cc = expandAddrs(parseAddrs($c));
         $bcc = expandAddrs(parseAddrs($b));
         if (isset($identity) && $identity != 'default') {
             $reply_to = getPref($data_dir, $username, 'reply_to' . $identity);
             $from = getPref($data_dir, $username, 'full_name' . $identity);
-            $from_addr = getPref($data_dir, $username, 
-                                 'email_address' . $identity);
+            $from_addr = getFrom();
         } else {
             $reply_to = getPref($data_dir, $username, 'reply_to');
             $from = getPref($data_dir, $username, 'full_name');
-            $from_addr = getPref($data_dir, $username, 'email_address');
-        }
-        
-        if ($from_addr == '') {
-            $from_addr = $popuser.'@'.$domain;
+            $from_addr = getFrom();
         }
         
         $to_list = getLineOfAddrs($to);
@@ -384,9 +384,15 @@ function write822Header ($fp, $t, $c, $b, $subject, $more_headers, $session, $rn
         
         $headerlength = strlen($header);
     }     
+
+       if ($headerrn != $rn) {
+               $header = str_replace($headerrn, $rn, $header);
+        $headerlength = strlen($header);
+               $headerrn = $rn;
+       }
     
     /* Write the header */
-    fputs ($fp, $header);
+    if ($fp) fputs ($fp, $header);
     
     return $headerlength;
 }
@@ -410,7 +416,7 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
         
         $body .= "Content-Transfer-Encoding: 8bit" . $rn . $rn;
         $body .= $passedBody . $rn . $rn;
-        fputs ($fp, $body);
+        if ($fp) fputs ($fp, $body);
         
         $attachmentlength = attachFiles($fp, $session, $rn);
         
@@ -418,12 +424,12 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n") {
             $postbody = ""; 
         }
         $postbody .= $rn . "--" . mimeBoundary() . "--" . $rn . $rn;
-        fputs ($fp, $postbody);
+        if ($fp) fputs ($fp, $postbody);
     } else {
         $body = $passedBody . $rn;
-        fputs ($fp, $body);
+        if ($fp) fputs ($fp, $body);
         $postbody = $rn;
-        fputs ($fp, $postbody);
+        if ($fp) fputs ($fp, $postbody);
     }
 
     return (strlen($body) + strlen($postbody) + $attachmentlength);
@@ -438,7 +444,7 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
      * spaces or other "weird" chars that would allow a user to
      * exploit the shell/pipe it is used in.
      */
-    $envelopefrom = "$popuser@$domain";
+    $envelopefrom = getFrom();
     $envelopefrom = ereg_replace("[[:blank:]]",'', $envelopefrom);
     $envelopefrom = ereg_replace("[[:space:]]",'', $envelopefrom);
     $envelopefrom = ereg_replace("[[:cntrl:]]",'', $envelopefrom);
@@ -458,7 +464,7 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
     $bodylength = writeBody($fp, $body, $session, "\n");
     
     pclose($fp);
-    
+
     return ($headerlength + $bodylength);
 }
 
@@ -853,13 +859,17 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     if ($useSendmail) {
         $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers, 
                                $session);
+        $body = ereg_replace("\n", "\r\n", $body);
     } else {
         $body = ereg_replace("\n", "\r\n", $body);
         $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, 
                            $session);
     }
     if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
-        if ($useSendmail) $body = ereg_replace("\n", "\r\n", $body);
+               $headerlength = write822Header (FALSE, $t, $c, $b, $subject, $more_headers, $session, "\r\n");
+               $bodylength = writeBody(FALSE, $body, $session, "\r\n");
+               $length = $headerlength + $bodylength;
+
         sqimap_append ($imap_stream, $sent_folder, $length);
         write822Header ($imap_stream, $t, $c, $b, $subject, $more_headers, 
                         $session);
@@ -937,5 +947,24 @@ function createReceiptHeaders($receipt) {
     return $receipt_headers;
 }
 
+/* Figure out what the 'From:' address is
+ */
+
+function getFrom() {
+    global $username, $popuser, $domain, $data_dir, $identity;
+    if (isset($identity) && $identity != 'default') {
+        $from_addr = getPref($data_dir, $username, 
+                             'email_address' . $identity);
+    }
+    else {
+        $from_addr = getPref($data_dir, $username, 'email_address');
+    }
+    
+    if (!$from_addr) {
+        $from_addr = "$popuser@$domain";
+    }
+    return $from_addr;
+}
+
 
 ?>