fix for Junk after literal error message from imap-server when mail is
[squirrelmail.git] / functions / smtp.php
index a2551ed..f1837c8 100644 (file)
@@ -77,7 +77,6 @@ function expandAddrs ($array) {
     return $array;
 }
 
-
 /* looks up aliases in the addressbook and expands them to
  * the RFC 821 valid RCPT address. ie <user@example.com>
  * Adds @$domain if it wasn't in the address book and if it
@@ -130,7 +129,8 @@ function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
                 if ( isset($info['remotefilename']) 
                      && $info['remotefilename'] != '') {
                     $remotefilename = $info['remotefilename'];
-                    if (function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
+                    if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && 
+                      function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
                         $remotefilename = 
                             $languages[$squirrelmail_language]['XTRA_CODE']('encode', $remotefilename);
                     }
@@ -153,22 +153,23 @@ function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
                 if (substr($filetype, 0, 5) == 'text/' ||
                     substr($filetype, 0, 8) == 'message/' ) {
                     $header .= $rn;
-                                       if ($fp) {
-                                           fputs ($fp, $header);
-                                       }
+                    if ($fp) {
+                       fputs ($fp, $header);
+                   }
                     $length += strlen($header);
+
                    if ($checkdot) {
                        $checkdot_begin=true;
                    } else {
                        $checkdot_begin=false;
                    }
+
                     while ($tmp = fgets($file, 4096)) {
                         $tmp = str_replace("\r\n", "\n", $tmp);
                         $tmp = str_replace("\r", "\n", $tmp);
-                       /* In order to remove the problem of users not able to create
-                        * messages with "." on a blank line, RFC821 has made provision
-                        * in section 4.5.2 (Transparency).
-                        */
+                        if ($rn == "\r\n"){
+                            $tmp = str_replace("\n", "\r\n", $tmp);
+                        }
                        if ($tmp{0} == '.' && $checkdot_begin) {
                            $tmp = '.' . $tmp;
                        }
@@ -176,26 +177,27 @@ function attachFiles ($fp, $session, $rn="\r\n", $checkdot = false) {
                            $tmp = str_replace("\n.","\n..",$tmp);
                        }
                        
-                        if ($rn == "\r\n"){
-                            $tmp = str_replace("\n", "\r\n", $tmp);
-                        }
                        $tmp_length = strlen($tmp);
-                       if ($tmp{$tmp_length-1} == "\n" && $checkdot) {
+                       if ($tmp_length && $tmp{$tmp_length-1} == "\n" && $checkdot) {
                            $checkdot_begin = true;
                        } else {
                            $checkdot_begin = false;
                        }
-                       if ($fp) {
-                          fputs($fp, $tmp);
+                       
+                       
+                        if ($fp) {
+                            fputs($fp, $tmp);
                         }
-                       $length += strlen($tmp_length);
+                        $length += $tmp_length;
                     }
-                   
-                    if (substr($tmp, strlen($tmp) - strlen($rn), strlen($rn)) != $rn) {
+                    if (substr($tmp, $tmp_length - strlen($rn), strlen($rn)) != $rn) {
                         if ($fp) {
                             fputs($fp, $rn);
+                           $length += strlen($rn);
                         }
                     }
+
+
                 } else {
                     $header .= "Content-Transfer-Encoding: base64" 
                         . "$rn" . "$rn";
@@ -492,6 +494,7 @@ function writeBody ($fp, $passedBody, $session, $rn="\r\n", $checkdot = false) {
     return (strlen($body) + strlen($postbody) + $attachmentlength);
 }
 
+
 /* Send mail using the sendmail command
  */
 function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
@@ -516,7 +519,7 @@ function sendSendmail($t, $c, $b, $subject, $body, $more_headers, $session) {
         $fp = popen (escapeshellcmd("$sendmail_path -t -f$envelopefrom"), "w");
     }
     
-    $headerlength = write822Header ($fp, $t, $c, $b, $subject, $body,
+    $headerlength = write822Header ($fp, $t, $c, $b, $subject, $body,  
                                     $more_headers, $session, "\n");
     $bodylength = writeBody($fp, $body, $session, "\n", true);
     
@@ -675,7 +678,7 @@ function sendSMTP($t, $c, $b, $subject, $body, $more_headers, $session) {
     /* Send the message */
     $headerlength = write822Header ($smtpConnection, $t, $c, $b, 
                                     $subject, $body, $more_headers, $session);
-    $bodylength = writeBody($smtpConnection, $body, $session, "\r\n", true);
+    $bodylength = writeBody($smtpConnection, $body, $session,"\r\n", true);
     
     fputs($smtpConnection, ".\r\n"); /* end the DATA part */
     $tmp = fgets($smtpConnection, 1024);
@@ -807,20 +810,19 @@ function calculate_references($refs, $inreplyto, $old_reply_to) {
     $refer = "";
     for ($i=1;$i<count($refs[0]);$i++) {
         if (!empty($refs[0][$i])) {
-            if (preg_match("/^References:(.+)$/UA", $refs[0][$i], $regs)) {
+            if (preg_match("/^References:(.+)$/UAi", $refs[0][$i], $regs)) {
                 $refer = trim($regs[1]);
             }
-            else {
-               $refer .= ' ' . trim($refs[0][$i]);
-           }
-            
+            else {   
+                $refer .= ' ' . trim($refs[0][$i]);
+            }
         }
     }
     $refer_a = explode(' ', $refer);
     $refer = '';
     foreach ($refer_a as $ref) {
        $ref = trim($ref);
-       if ($ref{0} == '<' && $ref{(strlen($ref)-1)} == '>') {
+       if ($ref && $ref{0} == '<' && $ref{(strlen($ref)-1)} == '>') {
           $refer .= $ref . ' ';
        }
     }
@@ -856,7 +858,7 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
 
     if (isset($reply_id) && $reply_id) {
         sqimap_mailbox_select ($imap_stream, $mailbox);
-        sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered', false);
+        sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered', true);
 
         /* Insert In-Reply-To and References headers if the
          * message-id of the message we reply to is set (longer than "<>")
@@ -903,53 +905,40 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     if ( $requestRecipt > 0) {
         $more_headers = array_merge($more_headers, createReceiptHeaders($requestRecipt));
     }
-
-    /* this is to catch all plain \n instances and
-     * replace them with \r\n.  All newlines were converted
-     * into just \n inside the compose.php file.
-     * But only if delimiter is, in fact, \r\n.
-     */
-    
     if ($MDN) {
         $more_headers["Content-Type"] = "multipart/report; ".
             "report-type=disposition-notification;";
     }
-
+    $imap_body = $body; 
     if ($useSendmail) {
-       /* In order to remove the problem of users not able to create
-        * messages with "." on a blank line, RFC821 has made provision
-        * in section 4.5.2 (Transparency).
+        /* In order to remove the problem of users not able to create
+         * messages with "." on a blank line, RFC821 has made provision
+         * in section 4.5.2 (Transparency).
          */
-        $body_sendmail = $body;
-        if (($body_sendmail{0} == '.')) {
-            $body_sendmail = '.' . $body_sendmail;
+        if (($body && $body{0} == '.')) {
+            $body = '.' . $body;
         }
-        $body_sendmail = str_replace("\n.","\n..",$body_sendmail);
-
-         $length = sendSendmail($t, $c, $b, $subject, $body_sendmail, $more_headers, 
+        $body = str_replace("\n.","\n..",$body);
+         $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);
-       /* In order to remove the problem of users not able to create
-        * messages with "." on a blank line, RFC821 has made provision
-        * in section 4.5.2 (Transparency).
-         */
-         $body_smtp = $body;
-        if (($body_smtp{0} == '.')) {
-            $body_smtp = '.' . $body_smtp;
-        }
-        $body_smtp = str_replace("\n.","\n..",$body_smtp);
-
-
-         $length = sendSMTP($t, $c, $b, $subject, $body_smtp, $more_headers, 
+       /* In order to remove the problem of users not able to create
+        * messages with "." on a blank line, RFC821 has made provision
+        * in section 4.5.2 (Transparency).
+        */
+       if (($body && $body{0} == '.')) {
+           $body = '.' . $body;
+       }
+       $body = str_replace("\n.","\n..",$body);
+        $length = sendSMTP($t, $c, $b, $subject, $body, $more_headers, 
                            $session);
     }
     if (sqimap_mailbox_exists ($imap_stream, $sent_folder)) {
-               $headerlength = write822Header (false, $t, $c, $b, $subject, $more_headers, $session, "\r\n");
-               $bodylength = writeBody(false, $body, $session, "\r\n");
-               $length = $headerlength + $bodylength;
-
+        $body = $imap_body;
+        $body = ereg_replace("\n", "\r\n", $body);
+       $headerlength = write822Header (false, $t, $c, $b, $subject, $body, $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, $body, $more_headers, 
                         $session);
@@ -963,7 +952,6 @@ function sendMessage($t, $c, $b, $subject, $body, $reply_id, $MDN,
     if ($length) {
         ClearAttachments($session);
     }
-
     return $length;
 }