Right-to-left fixes from Saleh Madi <webmail@p-i-s.com> (part 2)
[squirrelmail.git] / src / compose.php
index f95dd7e409a2400488ba0eb890d04c65851b2b97..0e2e83a6d269d9f201a6e3ee2ee688779e0085a9 100644 (file)
@@ -3,7 +3,7 @@
 /**
  * compose.php
  *
- * Copyright (c) 1999-2002 The SquirrelMail Project Team
+ * Copyright (c) 1999-2003 The SquirrelMail Project Team
  * Licensed under the GNU GPL. For full terms see the file COPYING.
  *
  * This code sends a mail.
@@ -28,6 +28,8 @@ require_once(SM_PATH . 'functions/mime.php');
 require_once(SM_PATH . 'functions/plugin.php');
 require_once(SM_PATH . 'functions/display_messages.php');
 require_once(SM_PATH . 'class/deliver/Deliver.class.php');
+require_once(SM_PATH . 'functions/addressbook.php');
+require_once(SM_PATH . 'functions/set_language_align.php');
 
 /* --------------------- Get globals ------------------------------------- */
 $username = $_SESSION['username'];
@@ -35,6 +37,8 @@ $onetimepad = $_SESSION['onetimepad'];
 $base_uri = $_SESSION['base_uri'];
 $delimiter = $_SESSION['delimiter'];
 
+$language_align = set_language_align();
+
 if (isset($_POST['return'])) {
     $html_addr_search_done = 'Use Addresses';
 }
@@ -104,7 +108,7 @@ $key = $_COOKIE['key'];
 
 function replyAllString($header) {
    global $include_self_reply_all, $username, $data_dir;
-   $excl_arr = array();
+   $excl_ar = array();
    /**
     * 1) Remove the addresses we'll be sending the message 'to'
     */
@@ -117,16 +121,16 @@ function replyAllString($header) {
     * TO list) only if $include_self_reply_all is turned off
     */
    if (!$include_self_reply_all) {
-       $email_address = trim(getPref($data_dir, $username, 'email_address'));
+       $email_address = strtolower(trim(getPref($data_dir, $username, 'email_address')));
        $excl_ar[$email_address] = '';
-    
        $idents = getPref($data_dir, $username, 'identities');
        if ($idents != '' && $idents > 1) {
+         $first_id = false;
           for ($i = 1; $i < $idents; $i ++) {
              $cur_email_address = getPref($data_dir, $username, 
                                          'email_address' . $i);
-             $cur_email_address = strtolower($cur_email_address);
-         $excl_ar[$cur_email_address] = '';
+             $cur_email_address = strtolower(trim($cur_email_address));
+             $excl_ar[$cur_email_address] = '';
          }
        }
    }
@@ -142,9 +146,9 @@ function replyAllString($header) {
    $url_replytoallcc = '';
    foreach( $url_replytoall_ar as $email => $personal) {
       if ($personal) {
-     $url_replytoallcc .= ", \"$personal\" <$email>";
+         $url_replytoallcc .= ", \"$personal\" <$email>";
       } else {
-     $url_replytoallcc .= ', '. $email;    
+         $url_replytoallcc .= ', '. $email;    
       }
    }
    $url_replytoallcc = substr($url_replytoallcc,2);
@@ -218,7 +222,6 @@ if (sqsession_is_registered('session_expired_post')) {
     if (!isset($mailbox)) {
         $mailbox = '';
     }
-
     if ($compose_new_win == '1') {
         compose_Header($color, $mailbox);
     } else {
@@ -264,7 +267,7 @@ if ($draft) {
      */
     set_my_charset();
     $composeMessage=$compose_messages[$session];
-    if (! sendMessage($composeMessage, true)) {
+    if (! deliverMessage($composeMessage, true)) {
         showInputForm($session);
         exit();
     } else {
@@ -297,6 +300,10 @@ if ($send) {
         $AttachFailure = saveAttachedFiles($session);
     }
     if (checkInput(false) && !isset($AttachFailure)) {
+               if ($mailbox == "All Folders") {
+                       /* We entered compose via the search results page */
+                       $mailbox="INBOX"; /* Send 'em to INBOX, that's safe enough */
+               }
         $urlMailbox = urlencode (trim($mailbox));
         if (! isset($passed_id)) {
             $passed_id = 0;
@@ -335,7 +342,7 @@ if ($send) {
         do_hook('compose_send');
         $composeMessage=$compose_messages[$session];
 
-       $Result = sendMessage($composeMessage);
+       $Result = deliverMessage($composeMessage);
         if (! $Result) {
             showInputForm($session);
             exit();
@@ -464,7 +471,6 @@ elseif (isset($sigappend)) {
        $composeMessage->entities = $new_entities;
        $compose_messages[$session] = $composeMessage;
        sqsession_register($compose_messages, 'compose_messages');
-//        setPref($data_dir, $username, 'attachments', serialize($attachments));
     }
     showInputForm($session);
 } else {
@@ -589,19 +595,25 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
         } else {
             $orig_from = '';
         }
+       $identities = array();
         if (!empty($idents) && $idents > 1) {
-            for ($i = 1; $i < $idents; $i++) {
-                $enc_from_name = '"'.
-                getPref($data_dir,
-                $username,
-                'full_name' . $i) .
-                '" <' . getPref($data_dir, $username,
-                'email_address' . $i) . '>';
-                if ($enc_from_name == $orig_from) {
+            $identities[]  = '"'. getPref($data_dir, $username, 'full_name') 
+             . '" <' .  getPref($data_dir, $username, 'email_address') . '>';
+           for ($i = 1; $i < $idents; $i++) {
+                $enc_from_name = '"'. 
+                   getPref($data_dir, $username, 'full_name' . $i) .
+                   '" <' . 
+                   getPref($data_dir, $username, 'email_address' . $i) . '>';
+                if ($enc_from_name == $orig_from && $i) {
                     $identity = $i;
                     break;
                 }
+               $identities[] = $enc_from_name;
             }
+           $identity_match = $orig_header->findAddress($identities);
+           if ($identity_match) {
+               $identity = $identity_match;
+           }
         }
 
         switch ($action) {
@@ -656,9 +668,11 @@ function newMail ($mailbox='', $passed_id='', $passed_ent_id='', $action='', $se
             $send_to_cc = replyAllString($orig_header);
         case ('reply'):
             $send_to = $orig_header->reply_to;
-            if (is_object($send_to)) {
-                $send_to = decodeHeader($send_to->getAddr_s('reply_to'));
-            } else {
+            if (is_array($send_to) && count($send_to)) {
+                $send_to = decodeHeader($orig_header->getAddr_s('reply_to'));
+            } else if (is_object($send_to)) { /* unnessecarry, just for falesafe purpose */
+                $send_to = decodeHeader($orig_header->getAddr_s('reply_to'));
+           } else {
                 $send_to = decodeHeader($orig_header->getAddr_s('from'));
             }
             $subject =  decodeHeader($orig_header->subject);
@@ -793,7 +807,7 @@ function getMessage_RFC822_Attachment($message, $composeMessage, $passed_id,
 }
 
 function showInputForm ($session, $values=false) {
-    global $send_to, $send_to_cc, $body,
+    global $send_to, $send_to_cc, $body, $language_align, 
            $passed_body, $color, $use_signature, $signature, $prefix_sig,
            $editor_size, $attachments, $subject, $newmail,
            $use_javascript_addr_book, $send_to_bcc, $passed_id, $mailbox,
@@ -801,7 +815,7 @@ function showInputForm ($session, $values=false) {
            $username, $data_dir, $identity, $draft_id, $delete_draft,
            $mailprio, $default_use_mdn, $mdn_user_support, $compose_new_win,
            $saved_draft, $mail_sent, $sig_first, $edit_as_new, $action, 
-           $username, $compose_messages, $composesession;
+           $username, $compose_messages, $composesession, $default_charset;
 
     $composeMessage = $compose_messages[$session];
 
@@ -813,7 +827,7 @@ function showInputForm ($session, $values=false) {
        $subject = $values['subject'];       
        $mailprio = $values['mailprio'];
        $body = $values['body'];
-       $identity = $values['identity'];
+       $identity = (int) $values['identity'];
     }
     
     if ($use_javascript_addr_book) {
@@ -827,6 +841,7 @@ function showInputForm ($session, $values=false) {
              '// --></SCRIPT>' . "\n\n";
     }
 
+
     echo "\n" . '<FORM name=compose action="compose.php" METHOD=POST ' .
          'ENCTYPE="multipart/form-data"';
     do_hook("compose_form");
@@ -853,10 +868,10 @@ function showInputForm ($session, $values=false) {
     if ($mail_sent == 'yes') {
         echo '<BR><CENTER><B>'. _("Your Message has been sent").'</CENTER></B>';
     }
-    echo '<TABLE ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
+    echo '<TABLE dir="' . $language_align['dir'] . '" ALIGN=center CELLSPACING=0 BORDER=0>' . "\n";
     if ($compose_new_win == '1') {
-        echo '<TABLE ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
-             '   <TR><TD></TD><TD ALIGN="RIGHT"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
+        echo '<TABLE dir="' . $language_align['dir'] . '" ALIGN=CENTER BGCOLOR="'.$color[0].'" WIDTH="100%" BORDER=0>'."\n" .
+             '   <TR dir="' . $language_align['dir'] . '"><TD dir="' . $language_align['dir'] . '"></TD><TD dir="' . $language_align['dir'] . '" ALIGN="' . $language_align['right'] . '"><INPUT TYPE="BUTTON" NAME="Close" onClick="return self.close()" VALUE='._("Close").'></TD></TR>'."\n";
     }
     if ($location_of_buttons == 'top') {
         showComposeButtonRow();
@@ -864,10 +879,10 @@ function showInputForm ($session, $values=false) {
 
     $idents = getPref($data_dir, $username, 'identities', 0);
     if ($idents > 1) {
-        echo '   <TR>' . "\n" .
-             '      <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
+        echo '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+             '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN="' . $language_align['right'] . '">' .
                     _("From:") . '</TD>' . "\n" .
-             '      <TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
+             '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" WIDTH="90%" ALIGN="' . $language_align['left'] . '">' . "\n" .
              '         <select name=identity>' . "\n" .
              '         <option value=default>' .
                        htmlspecialchars(getPref($data_dir, $username, 'full_name'));
@@ -892,34 +907,34 @@ function showInputForm ($session, $values=false) {
              '      </TD>' . "\n" .
              '   </TR>' . "\n";
     }
-    echo '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN=RIGHT>' .
+    echo '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" WIDTH="10%" ALIGN="' . $language_align['right'] . '">' .
                 _("To:") . '</TD>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" WIDTH="90%">' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" WIDTH="90%" ALIGN="' . $language_align['left'] . '">' . "\n" .
          '         <INPUT TYPE=text NAME="send_to" VALUE="' .
                    htmlspecialchars($send_to) . '" SIZE=60><BR>' . "\n" .
          '      </TD>' . "\n" .
          '   </TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
+         '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['right'] . '">' .
                 _("CC:") . '</TD>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['left'] . '">' . "\n" .
          '         <INPUT TYPE=text NAME="send_to_cc" SIZE=60 VALUE="' .
                    htmlspecialchars($send_to_cc) . '"><BR>' . "\n" .
          '      </TD>' . "\n" .
          '   </TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
+         '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['right'] . '">' .
                 _("BCC:") . '</TD>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['left'] . '">' . "\n" .
          '         <INPUT TYPE=text NAME="send_to_bcc" VALUE="' .
                 htmlspecialchars($send_to_bcc) . '" SIZE=60><BR>' . "\n" .
          '      </TD>' . "\n" .
          '   </TR>' . "\n" .
-         '   <TR>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=RIGHT>' .
+         '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['right'] . '">' .
                 _("Subject:") . '</TD>' . "\n" .
-         '      <TD BGCOLOR="' . $color[4] . '" ALIGN=LEFT>' . "\n";
+         '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" ALIGN="' . $language_align['left'] . '">' . "\n";
     echo '         <INPUT TYPE=text NAME=subject SIZE=60 VALUE="' .
                    htmlspecialchars($subject) . '">' . "\n" .
          '      </TD>' . "\n" .
@@ -930,20 +945,29 @@ function showInputForm ($session, $values=false) {
     }
 
     if ($compose_new_win == '1') {
-        echo '   <TR>' . "\n" .
-             '      <TD BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
+        echo '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+             '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[0] . '" COLSPAN=2 ALIGN=CENTER>' . "\n" .
              '         <TEXTAREA NAME=body ROWS=20 COLS="' .
                        $editor_size . '" WRAP="VIRTUAL">';
     }
     else {
-        echo '   <TR>' . "\n" .
-            '      <TD BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
+        echo '   <TR dir="' . $language_align['dir'] . '">' . "\n" .
+            '      <TD dir="' . $language_align['dir'] . '" BGCOLOR="' . $color[4] . '" COLSPAN=2>' . "\n" .
             '         &nbsp;&nbsp;<TEXTAREA NAME=body ROWS=20 COLS="' .
                       $editor_size . '" WRAP="VIRTUAL">';
     }
     if ($use_signature == true && $newmail == true && !isset($from_htmladdr_search)) {
+        if ($idents > 1) {
+            if ($identity == 'default') {
+                $no = 'g';
+        } else {
+            $no = $identity;
+        }
+        $signature = getSig($data_dir, $username, $no);
+    }
+
         if ($sig_first == '1') {
-            if ($charset == 'iso-2022-jp') {
+            if ($default_charset == 'iso-2022-jp') {
                 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
             } else {
             echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
@@ -952,7 +976,7 @@ function showInputForm ($session, $values=false) {
         }
         else {
             echo "\n\n".htmlspecialchars($body);
-            if ($charset == 'iso-2022-jp') {
+            if ($default_charset == 'iso-2022-jp') {
                 echo "\n\n".($prefix_sig==true? "-- \n":'').mb_convert_encoding($signature, 'EUC-JP');
             }else{
             echo "\n\n".($prefix_sig==true? "-- \n":'').htmlspecialchars($signature);
@@ -978,13 +1002,14 @@ function showInputForm ($session, $values=false) {
     }
 
     /* This code is for attachments */
+       if ((bool) ini_get('file_uploads')) {
     echo '   <TR>' . "\n" .
          '      <TD COLSPAN=2>' . "\n" .
-         '         <table width="100%" cellpadding="1" cellspacing="0" align="center"'.
+         '         <table dir="' . $language_align['dir'] . '" width="100%" cellpadding="1" cellspacing="0" align="center"'.
                    ' border="0" bgcolor="'.$color[9].'">' . "\n" .
          '            <TR>' . "\n" .
          '               <TD>' . "\n" .
-         '                 <table width="100%" cellpadding="3" cellspacing="0" align="center"'.
+         '                 <table dir="' . $language_align['dir'] . '" width="100%" cellpadding="3" cellspacing="0" align="center"'.
                            ' border="0">' . "\n" .
          '                    <TR>' . "\n" .
          '                       <TD VALIGN=MIDDLE ALIGN=RIGHT>' .
@@ -1005,16 +1030,14 @@ function showInputForm ($session, $values=false) {
                $attached_filename = decodeHeader($attachment->mime_header->getParameter('name'));
                $type = $attachment->mime_header->type0.'/'.
                        $attachment->mime_header->type1;
-               $s_a[] = '<input type="checkbox" name="delete[]" value="' . 
-                        $key . "\">\n" . $attached_filename . ' - ' . $type . 
-                        ' ('.show_readable_size( filesize( $attached_file ) ) 
-                        . ')<br>'."\n";
+               $s_a[] = '<table><tr><td><input type="checkbox" name="delete[]" value="' . 
+                        $key . "\"></td><td>\n" . $attached_filename . '</td><td>-</td><td> ' . $type . '</td><td>('.show_readable_size( filesize( $attached_file ) ) . ')</td></tr></table>'."\n";
            }
         }
     }
     if (count($s_a)) {
-       foreach ($s_a as $s) {
-          echo '<tr><td align=left colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
+     foreach ($s_a as $s) {
+       echo '<tr><td align="' . $language_align['left'] . '" colspan="2" bgcolor="' . $color[0] . '">'.$s.'</td></tr>';
        }         
        echo '<tr><td colspan="2"><input type="submit" name="do_delete" value="' .
             _("Delete selected attachments") . "\">\n" .
@@ -1026,11 +1049,12 @@ function showInputForm ($session, $values=false) {
          '         </TABLE>' . "\n" .
          '      </TD>' . "\n" .
          '   </TR>' . "\n";
-
+       } // End of file_uploads if-block
     /* End of attachment code */
     if ($compose_new_win == '1') {
         echo '</TABLE>'."\n";
     }
+
     echo '</TABLE>' . "\n" .
          '<input type="hidden" name="username" value="'. $username . "\">\n" .   
          '<input type=hidden name=action value="' . $action . "\">\n" .
@@ -1044,6 +1068,14 @@ function showInputForm ($session, $values=false) {
     echo '<input type=hidden name=composesession value="' . $composesession . "\">\n";
     echo '<input type=hidden name=querystring value="' . $_SERVER['QUERY_STRING'] . "\">\n";
     echo '</FORM>';
+    if (!(bool) ini_get('file_uploads')) {
+      /* File uploads are off, so we didn't show that part of the form.
+         To avoid bogus bug reports, tell the user why. */
+      echo 'Because PHP file uploads are turned off, you can not attach files ';
+      echo "to this message.  Please see your system administrator for details.\r\n";
+    }
+
+
     do_hook('compose_bottom');
     echo '</BODY></HTML>' . "\n";
 }
@@ -1146,16 +1178,12 @@ function saveAttachedFiles($session) {
         $full_localfilename = "$hashed_attachment_dir/$localfilename";
     }
 
+    // FIXME: we SHOULD prefer move_uploaded_file over rename because
+    // m_u_f works better with restricted PHP installes (safe_mode, open_basedir)
     if (!@rename($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
-       if (function_exists("move_uploaded_file")) {
             if (!@move_uploaded_file($_FILES['attachfile']['tmp_name'],$full_localfilename)) {
                return true;
            }
-       } else {
-           if (!@copy($_FILES['attachfile']['tmp_name'], $full_localfilename)) {
-                return true;
-            }
-       }
     }
     $message = $compose_messages[$session];
     $type = strtolower($_FILES['attachfile']['type']);
@@ -1220,7 +1248,7 @@ function getReplyCitation($orig_from) {
    The message also should be constructed by the message class.
 */
 
-function sendMessage($composeMessage, $draft=false) {
+function deliverMessage($composeMessage, $draft=false) {
     global $send_to, $send_to_cc, $send_to_bcc, $mailprio, $subject, $body,
            $username, $popuser, $usernamedata, $identity, $data_dir,
           $request_mdn, $request_dr, $default_charset, $color, $useSendmail,
@@ -1228,9 +1256,12 @@ function sendMessage($composeMessage, $draft=false) {
     global $imapServerAddress, $imapPort, $sent_folder, $key;
 
     $rfc822_header = $composeMessage->rfc822_header;
-    $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain);
-    $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain);
-    $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain);
+
+    $abook = addressbook_init(false, true);
+    
+    $rfc822_header->to = $rfc822_header->parseAddress($send_to,true, array(), '', $domain, array(&$abook,'lookup'));
+    $rfc822_header->cc = $rfc822_header->parseAddress($send_to_cc,true,array(), '',$domain, array(&$abook,'lookup'));
+    $rfc822_header->bcc = $rfc822_header->parseAddress($send_to_bcc,true, array(), '',$domain, array(&$abook,'lookup'));
     $rfc822_header->priority = $mailprio;
     $rfc822_header->subject = $subject;
     $special_encoding='';
@@ -1269,7 +1300,7 @@ function sendMessage($composeMessage, $draft=false) {
     if ($full_name) {
         $from = $rfc822_header->from[0];
        if (!$from->host) $from->host = $domain;
-       $from_addr = $full_name .' <'.$from->mailbox.'@'.$from->host.'>';
+       $from_addr = '"'.$full_name .'" <'.$from->mailbox.'@'.$from->host.'>';
         $rfc822_header->from = $rfc822_header->parseAddress($from_addr,true);
     }
     if ($reply_to) {
@@ -1315,18 +1346,20 @@ function sendMessage($composeMessage, $draft=false) {
     if (!$useSendmail && !$draft) {
        require_once(SM_PATH . 'class/deliver/Deliver_SMTP.class.php');
        $deliver = new Deliver_SMTP();
-       global $smtpServerAddress, $smtpPort, $use_authenticated_smtp, $pop_before_smtp;
-       if ($use_authenticated_smtp) {
-           global $key, $onetimepad;
-           $user = $username;
-           $pass = OneTimePadDecrypt($key, $onetimepad);
+       global $smtpServerAddress, $smtpPort, $pop_before_smtp, $smtp_auth_mech;
+
+       if ($smtp_auth_mech == 'none') {
+               $user = '';
+               $pass = '';
        } else {
-           $user = '';
-           $pass = '';
+               global $key, $onetimepad;
+               $user = $username;
+               $pass = OneTimePadDecrypt($key, $onetimepad);
        }
+
        $authPop = (isset($pop_before_smtp) && $pop_before_smtp) ? true : false;
        $stream = $deliver->initStream($composeMessage,$domain,0,
-                         $smtpServerAddress, $smtpPort, $authPop);
+                         $smtpServerAddress, $smtpPort, $user, $pass, $authPop);
     } elseif (!$draft) {
        require_once(SM_PATH . 'class/deliver/Deliver_SendMail.class.php');
        global $sendmail_path;
@@ -1343,7 +1376,7 @@ function sendMessage($composeMessage, $draft=false) {
           $length = $imap_deliver->mail($composeMessage);
           sqimap_append ($imap_stream, $draft_folder, $length);         
            $imap_deliver->mail($composeMessage, $imap_stream);
-          sqimap_append_done ($imap_stream);
+          sqimap_append_done ($imap_stream, $draft_folder);
           sqimap_logout($imap_stream);
           unset ($imap_deliver);
           return $length;
@@ -1359,7 +1392,9 @@ function sendMessage($composeMessage, $draft=false) {
        $succes = $deliver->finalizeStream($stream);
     }
     if (!$succes) {
-        $msg  = $deliver->dlv_msg . '<br>Server replied: '.$deliver->dlv_ret_nr;
+        $msg  = $deliver->dlv_msg . '<br>' .
+                _("Server replied: ") . $deliver->dlv_ret_nr . ' '.
+                $deliver->dlv_server_msg;
         plain_error_message($msg, $color);
     } else {
         unset ($deliver);
@@ -1370,7 +1405,7 @@ function sendMessage($composeMessage, $draft=false) {
            require_once(SM_PATH . 'class/deliver/Deliver_IMAP.class.php');
            $imap_deliver = new Deliver_IMAP();
            $imap_deliver->mail($composeMessage, $imap_stream);
-           sqimap_append_done ($imap_stream);
+           sqimap_append_done ($imap_stream, $sent_folder);
            unset ($imap_deliver);
        }
        global $passed_id, $mailbox, $action;