* I really didn't like the old attachment code
[squirrelmail.git] / functions / smtp.php
index 8579e9a14f794e06de35c886fd03cbff0ca8b0e8..0c66f44e96779aecea0d956e03ef2925b49ed5d9 100644 (file)
@@ -8,6 +8,8 @@
     **/
 
    $smtp_php = true;
+   if (!isset($addressbook_php))
+      include('../functions/addressbook.php');
 
    // This should most probably go to some initialization...
    if (ereg("^([^@%/]+)[@%/](.+)$", $username, $usernamedata)) {
          return false;
    }
 
+   // looks up aliases in the addressbook and expands them to
+   // the full address.
+   function expandAddrs ($array) {
+      $abook = addressbook_init();
+      for ($i=0; $i < count($array); $i++) {
+         $result = $abook->lookup($array[$i]);
+         $ret = "";
+         if (isset($result['email'])) {
+            if (isset($result['name'])) {
+               $ret = '"'.$result['name'].'" ';
+            }
+            $ret .= '<'.$result['email'].'>';
+            $array[$i] = $ret;
+         }
+        else
+        {
+           $array[$i] = '<' . $array[$i] . '>';
+        }
+      }
+      return $array;
+   }
+
    // Attach the files that are due to be attached
    function attachFiles ($fp) {
       global $attachments, $attachment_dir;
       $length = 0;
 
       if (isMultipart()) {
-         reset($attachments);
-         while (list($localname, $remotename) = each($attachments)) {
-            // This is to make sure noone is giving a filename in another
-            // directory
-            $localname = ereg_replace ("\\/", '', $localname);
-            
-            $fileinfo = fopen ($attachment_dir.$localname.'.info', 'r');
-            $filetype = fgets ($fileinfo, 8192);
-            fclose ($fileinfo);
-            $filetype = trim ($filetype);
-            if ($filetype=='')
+         foreach ($attachments as $info)
+        {
+           $filetype = $info['type'];
+            if ($filetype == '')
                $filetype = 'application/octet-stream';
             
             $header = '--'.mimeBoundary()."\r\n";
-            $header .= "Content-Type: $filetype;name=\"$remotename\"\r\n";
-            $header .= "Content-Disposition: attachment; filename=\"$remotename\"\r\n";
+            $header .= "Content-Type: $filetype; name=\"" .
+               $info['remotefilename'] . "\"\r\n";
+            $header .= "Content-Disposition: attachment; filename=\"" .
+               $info['remotefilename'] . "\"\r\n";
             $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
             fputs ($fp, $header);
             $length += strlen($header);
             
-            $file = fopen ($attachment_dir.$localname, 'r');
+            $file = fopen ($attachment_dir . $info['localfilename'], 'r');
             while ($tmp = fread($file, 570)) {
                $encoded = chunk_split(base64_encode($tmp));
                $length += strlen($encoded);
       static $mimeBoundaryString;
 
       if ($mimeBoundaryString == "") {
-         $mimeBoundaryString = GenerateRandomString(70, '\'()+,-./:=?_', 7);
+         $mimeBoundaryString = "----=_" . 
+            GenerateRandomString(60, '\'()+,-./:=?_', 7);
       }
 
       return $mimeBoundaryString;
       static $header, $headerlength;
 
       if ($header == '') {
-         $to = parseAddrs($t);
-         $cc = parseAddrs($c);
-         $bcc = parseAddrs($b);
+         $to = expandAddrs(parseAddrs($t));
+         $cc = expandAddrs(parseAddrs($c));
+         $bcc = expandAddrs(parseAddrs($b));
          $reply_to = getPref($data_dir, $username, 'reply_to');
          $from = getPref($data_dir, $username, 'full_name');
          $from_addr = getPref($data_dir, $username, 'email_address');
          $bcc_list = getLineOfAddrs($bcc);
 
          /* Encoding 8-bit characters and making from line */
-         $subject = sqStripSlashes(encodeHeader($subject));
+         $subject = encodeHeader($subject);
          if ($from == '')
             $from = "<$from_addr>";
          else
          $header .= "Date: $date\r\n";
          $header .= "Subject: $subject\r\n";
          $header .= "From: $from\r\n";
-         $header .= "To: $to_list \r\n";    // Who it's TO
+         $header .= "To: $to_list\r\n";    // Who it's TO
 
         /* Insert headers from the $more_headers array */
         if(is_array($more_headers)) {
             $body .= "Content-Type: text/plain\r\n";
 
          $body .= "Content-Transfer-Encoding: 8bit\r\n\r\n";
-         $body .= sqStripSlashes($passedBody) . "\r\n\r\n";
+         $body .= $passedBody . "\r\n\r\n";
          fputs ($fp, $body);
 
          $attachmentlength = attachFiles($fp);
 
+         if (!isset($postbody)) $postbody = "";
          $postbody .= "\r\n--".mimeBoundary()."--\r\n\r\n";
          fputs ($fp, $postbody);
       } else {
-         $body = sqStripSlashes($passedBody) . "\r\n";
+         $body = $passedBody . "\r\n";
          fputs ($fp, $body);
          $postbody = "\r\n";
          fputs ($fp, $postbody);
       global $username, $popuser, $domain, $version, $smtpServerAddress, $smtpPort,
          $data_dir, $color;
 
-      $to = parseAddrs($t);
-      $cc = parseAddrs($c);
-      $bcc = parseAddrs($b);
+      $to = expandAddrs(parseAddrs($t));
+      $cc = expandAddrs(parseAddrs($c));
+      $bcc = expandAddrs(parseAddrs($b));
       $from_addr = getPref($data_dir, $username, 'email_address');
 
       if (!$from_addr)
       errorCheck($tmp, $smtpConnection);
 
       /** Ok, who is sending the message? */
-      fputs($smtpConnection, "MAIL FROM:<$from_addr>\r\n");
+      fputs($smtpConnection, "MAIL FROM: <$from_addr>\r\n");
       $tmp = fgets($smtpConnection, 1024);
       errorCheck($tmp, $smtpConnection);
 
       /** send who the recipients are */
       for ($i = 0; $i < count($to); $i++) {
-         fputs($smtpConnection, "RCPT TO:<$to[$i]>\r\n");
+         fputs($smtpConnection, "RCPT TO: $to[$i]\r\n");
          $tmp = fgets($smtpConnection, 1024);
          errorCheck($tmp, $smtpConnection);
       }
       for ($i = 0; $i < count($cc); $i++) {
-         fputs($smtpConnection, "RCPT TO:<$cc[$i]>\r\n");
+         fputs($smtpConnection, "RCPT TO: $cc[$i]\r\n");
          $tmp = fgets($smtpConnection, 1024);
          errorCheck($tmp, $smtpConnection);
       }
       for ($i = 0; $i < count($bcc); $i++) {
-         fputs($smtpConnection, "RCPT TO:<$bcc[$i]>\r\n");
+         fputs($smtpConnection, "RCPT TO: $bcc[$i]\r\n");
          $tmp = fgets($smtpConnection, 1024);
          errorCheck($tmp, $smtpConnection);
       }
 
       $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 1);
 
-      if ($reply_id) {
+      if (isset($reply_id) && $reply_id) {
          sqimap_mailbox_select ($imap_stream, $mailbox);
          sqimap_messages_flag ($imap_stream, $reply_id, $reply_id, 'Answered');
 
       // 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 = ereg_replace("\n\.", "\n\.\.", $body);
-         $body = ereg_replace("^\.", "\.\.", $body);
+         $body = ereg_replace("\n\\.", "\n..", $body);
+         $body = ereg_replace("^\\.", "..", $body);
 
       // this is to catch all plain \n instances and
       // replace them with \r\n.  
       $body = ereg_replace("\r\n", "\n", $body);
       $body = ereg_replace("\n", "\r\n", $body);
       
-      // Make sure that $t, $c, and $b do not contain newlines.
-      // I don't think they are needed anymore
-      //$t = ereg_replace("[\n|\r]", "", $t);
-      //$c = ereg_replace("[\n|\r]", "", $c);
-      //$b = ereg_replace("[\n|\r]", "", $b);
-
       if ($useSendmail) {
          $length = sendSendmail($t, $c, $b, $subject, $body, $more_headers);
       } else {
       }
       sqimap_logout($imap_stream);
       // Delete the files uploaded for attaching (if any).
-      deleteAttachments();
+      ClearAttachments();
    }
 
 ?>