String fix.
[squirrelmail.git] / plugins / mail_fetch / fetch.php
index eb00fff5974f127b65f4219e7d3cf1a47ae48caa..95bb9b57354e8e2a77ffdc4e80da3c0d9ba99125 100644 (file)
 <?php
 
-   /**
-    **  mail_fetch/fetch.php
-    **
-    **  Copyright (c) 1999-2002 The SquirrelMail Project Team
-    **  Licensed under the GNU GPL. For full terms see the file COPYING.
-    **
-    **  Fetch code.
-    **
-    **  $Id$
-    **/
-
-    chdir('..');
-    require_once('../src/validate.php');
-    require_once('../functions/page_header.php');
-    require_once('../functions/imap.php');
-    require_once('../src/load_prefs.php');
-    require_once('../plugins/mail_fetch/class.POP3.php');
-    require_once('../functions/i18n.php');
-    require_once( '../plugins/mail_fetch/functions.php' );
-
-
-    function Mail_Fetch_Status($msg) {
-        echo '<table width="90%"><tr><td>' .
-            htmlspecialchars( $msg ) .
-            '</td></tr></table>';
-        flush();
+/**
+ * mail_fetch/fetch.php
+ *
+ * Fetch code.
+ *
+ * @copyright &copy; 1999-2009 The SquirrelMail Project Team
+ * @license http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @version $Id$
+ * @package plugins
+ * @subpackage mail_fetch
+ */
+
+/**
+ * Include the SquirrelMail initialization file.
+ */
+require('../../include/init.php');
+
+include_once(SM_PATH . 'functions/imap_general.php');
+include_once(SM_PATH . 'plugins/mail_fetch/functions.php' );
+
+// don't load this page if this plugin is not enabled
+//
+global $plugins;
+if (!in_array('mail_fetch', $plugins)) exit;
+
+/* globals */
+sqgetGlobalVar('delimiter',  $delimiter,  SQ_SESSION);
+/* end globals */
+
+/**
+ * @param string $msg mail
+ */
+function Mail_Fetch_Status($msg) {
+    echo html_tag( 'table',
+             html_tag( 'tr',
+                 html_tag( 'td', htmlspecialchars( $msg ) , 'left' )
+                 ),
+             '', '', 'width="90%"' );
+    flush();
+}
+
+/**
+ * @return array
+ */
+function Mail_Fetch_Servers() {
+    global $data_dir, $username;
+
+    $mailfetch = array();
+    $mailfetch['server_number'] = getPref($data_dir, $username, "mailfetch_server_number");
+    if (!isset($mailfetch['server_number']) || ($mailfetch['server_number'] < 1)) {
+        $mailfetch['server_number'] = 0;
     }
+    $mailfetch['cypher'] = getPref($data_dir, $username, "mailfetch_cypher");
 
-    displayPageHeader($color, 'None');
-
-    $mailfetch_server_number = getPref($data_dir, $username, "mailfetch_server_number");
-    if (!isset($mailfetch_server_number)) $mailfetch_server_number=0;
-    $mailfetch_cypher = getPref($data_dir, $username, "mailfetch_cypher");
-    if ($mailfetch_server_number<1) $mailfetch_server_number=0;
-    for ($i=0;$i<$mailfetch_server_number;$i++) {
-        $mailfetch_server_[$i] = getPref($data_dir, $username, "mailfetch_server_$i");
-        $mailfetch_alias_[$i] = getPref($data_dir, $username, "mailfetch_alias_$i");
-        $mailfetch_user_[$i] = getPref($data_dir, $username, "mailfetch_user_$i");
-        $mailfetch_pass_[$i] = getPref($data_dir, $username, "mailfetch_pass_$i");
-        $mailfetch_lmos_[$i] = getPref($data_dir, $username, "mailfetch_lmos_$i");
-        $mailfetch_login_[$i] = getPref($data_dir, $username, "mailfetch_login_$i");
-        $mailfetch_uidl_[$i] = getPref($data_dir, $username, "mailfetch_uidl_$i");
-        $mailfetch_subfolder_[$i] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
-        if( $mailfetch_cypher == 'on' ) {
-            $mailfetch_pass_[$i] = decrypt( $mailfetch_pass_[$i] );
+    for ($i = 0; $i < $mailfetch['server_number']; $i++) {
+        $mailfetch[$i]['server'] = getPref($data_dir, $username, "mailfetch_server_$i");
+        $mailfetch[$i]['port']   = getPref($data_dir, $username, "mailfetch_port_$i");
+        $mailfetch[$i]['alias']  = getPref($data_dir, $username, "mailfetch_alias_$i");
+        $mailfetch[$i]['user']   = getPref($data_dir, $username, "mailfetch_user_$i");
+        $mailfetch[$i]['pass']   = getPref($data_dir, $username, "mailfetch_pass_$i");
+        if($mailfetch['cypher'] == 'on') {
+            $mailfetch[$i]['pass'] = decrypt($mailfetch[$i]['pass']);
         }
-    }
-    
-    echo '<br><center>';
-    
-    echo '<TABLE WIDTH=95% COLS=1 ALIGN=CENTER>' .
-            "<TR><TD BGCOLOR=\"$color[0]\" ALIGN=CENTER><b>" . _("Remote POP server Fetching Mail") . '</b></TD></TR>' .
-         '</TABLE>';
-    
-    if (!isset( $server_to_fetch ) ) {
-        
-        echo '<font size=-5><BR></font>' .
-             "<form action=\"$PHP_SELF\" METHOD=POST TARGET=_self>" .
-             '<TABLE WIDTH=70% COLS=2 ALIGN=CENTER>' .
-                '<TR>' .
-                    '<TD ALIGN=RIGHT>' . _("Select Server:") . ' &nbsp; &nbsp; </TD>' .
-                    '<TD><SELECT NAME=server_to_fetch SIZE=1>' .
-                        '<OPTION VALUE="all" SELECTED>..' . _("All") . "...\n";
-        for ($i=0;$i<$mailfetch_server_number;$i++) {
-             echo "<OPTION VALUE=\"$i\">" .
-                  (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]);
-        } 
-        echo            '</SELECT>' .
-                    '</TD>' .
-                '</TR>';
-        
-        //if password not set, ask for it
-        for ($i=0;$i<$mailfetch_server_number;$i++) {
-             if ($mailfetch_pass_[$i]=='') {
-                  echo '<tr>' .
-                       '<TD ALIGN=RIGHT>' . _("Password for") . ' <B>' . (($mailfetch_alias_[$i]=='')?$mailfetch_server_[$i]:$mailfetch_alias_[$i]) . '</B>: &nbsp; &nbsp; </TD>' .
-                       "<TD><INPUT TYPE=PASSWORD NAME=pass_$i></TD>" .
-                       '</TR>';
-             }
+        if ($mailfetch[$i]['pass'] == '') {
+            sqgetGlobalVar("pass_$i", $mailfetch[$i]['pass'], SQ_POST);
+        }
+        $mailfetch[$i]['lmos']   = getPref($data_dir, $username, "mailfetch_lmos_$i");
+        $mailfetch[$i]['login']  = getPref($data_dir, $username, "mailfetch_login_$i");
+        $mailfetch[$i]['uidl']   = getPref($data_dir, $username, "mailfetch_uidl_$i");
+        $mailfetch[$i]['subfolder'] = getPref($data_dir, $username, "mailfetch_subfolder_$i");
+        if($mailfetch[$i]['alias'] == '') {
+            $mailfetch[$i]['alias'] == $mailfetch[$i]['server'];
         }
-        echo '<TR>' .
-                '<TD>&nbsp;</TD>' .
-                '<TD><input type=submit name=submit_mailfetch value="' . _("Fetch Mail"). '"></TD>'.
-                '</TR>' .
-             '</TABLE></form>';
-        exit();
+        // Authentication type (added in 1.5.2)
+        $mailfetch[$i]['auth'] = getPref($data_dir, $username, "mailfetch_auth_$i",MAIL_FETCH_AUTH_USER);
+        // Connection type (added in 1.5.2)
+        $mailfetch[$i]['type'] = getPref($data_dir, $username, "mailfetch_type_$i",MAIL_FETCH_USE_PLAIN);
     }
+    return $mailfetch;
+}
 
-    if ( $server_to_fetch == 'all' ) {
-        $i_start = 0;
-        $i_stop = $mailfetch_server_number;
-    } else {
-        $i_start = $server_to_fetch;
-        $i_stop = $i_start+1;
+/**
+ * @param array $mailfetch
+ */
+function Mail_Fetch_Select_Server($mailfetch) {
+    global $PHP_SELF;
+
+    echo '<font size="-5"><br /></font>' .
+        '<form action="'.$PHP_SELF.'" method="post" target="_self">' .
+        html_tag( 'table', '', 'center', '', 'width="70%" cols="2"' ) .
+        html_tag( 'tr' ) .
+        html_tag( 'td', _("Select Server:") . ' &nbsp; &nbsp;', 'right' ) .
+        html_tag( 'td', '', 'left' ) .
+        '<select name="server_to_fetch" size="1">' .
+        '<option value="all" selected="selected">..' . _("All") . "...\n";
+    for ($i = 0;$i < $mailfetch['server_number'];$i++) {
+        echo "<option value=\"$i\">" .
+            htmlspecialchars($mailfetch[$i]['alias']) .
+            '</option>' . "\n";
     }
-    
-    for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
-        $mailfetch_server=$mailfetch_server_[$i_loop];
-        $mailfetch_user=$mailfetch_user_[$i_loop];
-        if ($mailfetch_pass_[$i_loop]=="") {
-            $tmp="pass_$i_loop";
-            $mailfetch_pass=$$tmp;
-        } else {
-            $mailfetch_pass=$mailfetch_pass_[$i_loop];
-        }
-        $mailfetch_lmos=$mailfetch_lmos_[$i_loop];
-        $mailfetch_login=$mailfetch_login_[$i_loop];
-        $mailfetch_uidl=$mailfetch_uidl_[$i_loop];
-        $mailfetch_subfolder=$mailfetch_subfolder_[$i_loop];
-        
-        
-        $pop3 = new POP3($mailfetch_server, 60);
-        
-        echo "<br><table width=\"90%\"><tr bgcolor=\"$color[9]\"><td><b>" . 
-            _("Fetching from ") . 
-            (($mailfetch_alias_[$i_loop] == '')?$mailfetch_server:$mailfetch_alias_[$i_loop]) . 
-            "</b></td></tr></table>";
-          
-        flush();
-        
-        if (!$pop3->connect($mailfetch_server)) {
-            Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
-            continue;
+    echo            '</select>' .
+        '</td>' .
+        '</tr>';
+
+    //if password not set, ask for it
+    for ($i = 0;$i < $mailfetch['server_number'];$i++) {
+        if ($mailfetch[$i]['pass'] == '') {
+            echo html_tag( 'tr',
+                     html_tag( 'td', _("Password for") . ' <b>' .
+                         htmlspecialchars($mailfetch[$i]['alias']) .
+                         '</b>: &nbsp; &nbsp; ',
+                         'right' ) .
+                     html_tag( 'td', '<input type="password" name="pass_' . $i . '" />', 'left' )
+                           );
         }
-        
-        Mail_Fetch_Status(_("Opening IMAP server"));
-        $imap_stream = sqimap_login($username, $key, $imapServerAddress, $imapPort, 10);
-        
-        Mail_Fetch_Status(_("Opening POP server"));
-        $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
-        if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
-            Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
-            continue;
+    }
+    echo html_tag( 'tr',
+             html_tag( 'td', '&nbsp;' ) .
+             html_tag( 'td', '<input type="submit" name="submit_mailfetch" value="' . _("Fetch Mail"). '" />', 'left' )
+                   ) .
+        '</table></form>';
+}
+
+$mailfetch = Mail_Fetch_Servers();
+displayPageHeader($color);
+
+echo '<br />';
+
+echo html_tag( 'table',
+         html_tag( 'tr',
+             html_tag( 'td', '<b>' . _("Remote POP server Fetching Mail") . '</b>', 'center', $color[0] )
+                   ) ,
+               'center', '', 'width="95%" cols="1"' );
+
+
+/* there are no servers defined yet... */
+if($mailfetch['server_number'] == 0) {
+//FIXME: do not echo directly to browser -- use templates only
+    echo '<p>' . _("No POP3 servers configured yet.") . '</p>';
+    echo makeInternalLink('plugins/mail_fetch/options.php',
+                        _("Click here to go to the options page.") );
+    $oTemplate->display('footer.tpl');
+    exit();
+}
+
+// get $server_to_fetch from globals, if not set display a choice to the user
+if (! sqgetGlobalVar('server_to_fetch', $server_to_fetch, SQ_POST) ) {
+    Mail_Fetch_Select_Server($mailfetch);
+    $oTemplate->display('footer.tpl');
+    exit();
+}
+
+if ( $server_to_fetch == 'all' ) {
+    $i_start = 0;
+    $i_stop  = $mailfetch['server_number'];
+} else {
+    $i_start = $server_to_fetch;
+    $i_stop  = $i_start+1;
+}
+
+for ($i_loop=$i_start;$i_loop<$i_stop;$i_loop++) {
+    $mailfetch_server = $mailfetch[$i_loop]['server'];
+    $mailfetch_port   = $mailfetch[$i_loop]['port'];
+    $mailfetch_user   = $mailfetch[$i_loop]['user'];
+    $mailfetch_pass   = $mailfetch[$i_loop]['pass'];
+    $mailfetch_lmos   = $mailfetch[$i_loop]['lmos'];
+    $mailfetch_login  = $mailfetch[$i_loop]['login'];
+    $mailfetch_uidl   = $mailfetch[$i_loop]['uidl'];
+    $mailfetch_subfolder = $mailfetch[$i_loop]['subfolder'];
+    $mailfetch_auth = $mailfetch[$i_loop]['auth'];
+    $mailfetch_type = $mailfetch[$i_loop]['type'];
+
+    echo '<br />' .
+        html_tag( 'table',
+            html_tag( 'tr',
+                html_tag( 'td', '<b>' .
+                    sprintf(_("Fetching from %s"),
+                        htmlspecialchars($mailfetch[$i_loop]['alias'])) .
+                    '</b>',
+                'center' ) ,
+            '', $color[9] ) ,
+        '', '', 'width="90%"' );
+
+    flush();
+
+    $pop3 = new mail_fetch(array('host'    => $mailfetch_server,
+                                 'port'    => $mailfetch_port,
+                                 'auth'    => $mailfetch_auth,
+                                 'tls'     => $mailfetch_type,
+                                 'timeout' => 60));
+
+    if (!empty($pop3->error)) {
+        Mail_Fetch_Status($pop3->error);
+        continue;
+    }
+
+    Mail_Fetch_Status(_("Opening IMAP server"));
+    $imap_stream = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
+
+    // check if destination folder is not set, is not subscribed and is not \noselect folder
+    if($mailfetch_subfolder == '' ||
+       ! mail_fetch_check_folder($imap_stream,$mailfetch_subfolder)) {
+        $mailfetch_subfolder = 'INBOX';
+    }
+
+    Mail_Fetch_Status(_("Opening POP server"));
+
+    /* log into pop server*/
+    if (! $pop3->login($mailfetch_user, $mailfetch_pass)) {
+        Mail_Fetch_Status(_("Login Failed:") . ' ' . htmlspecialchars($pop3->error));
+        continue;
+    }
+
+    $aMsgStat = $pop3->command_stat();
+    if (is_bool($aMsgStat)) {
+        Mail_Fetch_Status(_("Can't get mailbox status:") . ' ' . htmlspecialchars($pop3->error) );
+        continue;
+    }
+
+    $Count = $aMsgStat['count'];
+
+    $i = 1;
+
+    if ($Count>0) {
+        // If we leave mails on server, try using UIDL
+        if ($mailfetch_lmos == 'on') {
+            Mail_Fetch_Status(_("Fetching UIDL..."));
+            $msglist = $pop3->command_uidl();
+            if (is_bool($msglist)) {
+                Mail_Fetch_Status(_("Server does not support UIDL.") . ' '.htmlspecialchars($pop3->error));
+                // User asked to leave mails on server, but we can't do that.
+                $pop3->command_quit();
+                continue;
+                // $mailfetch_lmos = 'off';
+            } else {
+                // calculate number of new mails
+                for ($j = 1; $j <= sizeof($msglist); $j++) {
+                    // do strict comparison ('1111.10' should not be equal to '1111.100')
+                    if ($msglist[$j] === $mailfetch_uidl) {
+                        $i = $j+1;
+                        break;
+                    }
+                }
+            }
         }
-        
-        //   register_shutdown_function($pop3->quit());
-        
-        $msglist = $pop3->uidl();
-
-        $i = 1;
-        for ($j = 1; $j < sizeof($msglist); $j++) {
-           if ($msglist["$j"] == $mailfetch_uidl) {
-                $i = $j+1;
-                break;
-           }
+        // fetch list of mails with LIST
+        // we can use else control, but we can also set $mailfetch_lmos 
+        // to off if server does not support UIDL.
+        if ($mailfetch_lmos != 'on') {
+            Mail_Fetch_Status(_("Fetching list of mails..."));
+            $msglist = $pop3->command_list();
         }
+    }
 
-        if ($Count < $i) {
-            Mail_Fetch_Status(_("Login OK: No new messages"));
-            $pop3->quit();
-            continue;
-        }
-        if ($Count == 0) {
-            Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
-            $pop3->quit();
+    if ($Count < $i) {
+        Mail_Fetch_Status(_("Login OK: No new mails"));
+        $pop3->command_quit();
+        continue;
+    }
+    if ($Count == 0) {
+        Mail_Fetch_Status(_("Login OK: Inbox EMPTY"));
+        $pop3->command_quit();
+        continue;
+    } else {
+        $newmsgcount = $Count - $i + 1;
+        Mail_Fetch_Status(sprintf(ngettext("Login OK: Inbox contains %s mail",
+                                           "Login OK: Inbox contains %s mails",$newmsgcount), $newmsgcount));
+    }
+
+    if ($mailfetch_lmos == 'on') {
+        Mail_Fetch_Status(_("Leaving mails on server..."));
+    } else {
+        Mail_Fetch_Status(_("Deleting mails from server..."));
+    }
+
+    for (; $i <= $Count; $i++) {
+        Mail_Fetch_Status(sprintf(_("Fetching mail %s."), $i));
+
+        if (!ini_get('safe_mode'))
+            set_time_limit(20); // 20 seconds per mail max
+
+        $Message = $pop3->command_retr($i);
+
+        if (is_bool($Message)) {
+            Mail_Fetch_Status(htmlspecialchars($pop3->error));
             continue;
-        } else {
-            $newmsgcount = $Count - $i + 1;
-            Mail_Fetch_Status(_("Login OK: Inbox contains [") . $newmsgcount . _("] messages"));
         }
-        
-        Mail_Fetch_Status(_("Fetching UIDL..."));
-        // Faster to get them all at once
-        $mailfetch_uidl = $pop3->uidl();
-        
-        if (! is_array($mailfetch_uidl) && $mailfetch_lmos == 'on')
-            Mail_Fetch_Status(_("Server does not support UIDL."));
 
-        if ($mailfetch_lmos == 'on') {
-            Mail_Fetch_Status(_("Leaving Mail on Server..."));
-        } else {
-            Mail_Fetch_Status(_("Deleting messages from server..."));
-        }
-        
-        for (; $i <= $Count; $i++) {
-            Mail_Fetch_Status(_("Fetching message ") . "$i" );
-            set_time_limit(20); // 20 seconds per message max
-            $Message = "";
-            $MessArray = $pop3->get($i);
-            
-            while ( (!$MessArray) or (gettype($MessArray) != "array")) {
-                 Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR);
-                 // re-connect pop3
-                 Mail_Fetch_Status(_("Server error...Disconnect"));
-                $pop3->quit();
-                 Mail_Fetch_Status(_("Re-connect from dead connectoin"));
-                 if (!$pop3->connect($mailfetch_server)) {
-                     Mail_Fetch_Status(_("Oops, ") . $pop3->ERROR );
-                     Mail_Fetch_Status(_("Saving UIDL"));
-                     setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
-
-                     continue;
-                 }
-                 $Count = $pop3->login($mailfetch_user, $mailfetch_pass);
-                 if (($Count == false || $Count == -1) && $pop3->ERROR != '') {
-                     Mail_Fetch_Status(_("Login Failed:") . ' ' . $pop3->ERROR );
-                     Mail_Fetch_Status(_("Saving UIDL"));
-                     setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
-
-                     continue;
-                 }
-                 Mail_Fetch_Status(_("Re-fetching message ") . "$i" );
-                $MessArray = $pop3->get($i);
-
-            } // end while
-            
-            while (list($lineNum, $line) = each ($MessArray)) {
-                 $Message .= $line;
-            }
-            
-            if ($mailfetch_subfolder=="") {
-                fputs($imap_stream, "A3$i APPEND INBOX {" . (strlen($Message) - 1) . "}\r\n");
+        fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . strlen($Message) . "}\r\n");
+        $Line = fgets($imap_stream, 1024);
+        if (substr($Line, 0, 1) == '+') {
+            fputs($imap_stream, $Message);
+            fputs($imap_stream, "\r\n");
+            sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
+            $response=(implode('',$response));
+            $message=(implode('',$message));
+            if ($response != 'OK') {
+                Mail_Fetch_Status(_("Error Appending Mail!")." ".htmlspecialchars($message) );
+                Mail_Fetch_Status(_("Closing POP"));
+                $pop3->command_quit();
+                Mail_Fetch_Status(_("Logging out from IMAP"));
+                sqimap_logout($imap_stream);
+
+                if ($mailfetch_lmos == 'on') {
+                    Mail_Fetch_Status(_("Saving UIDL"));
+                    setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
+                }
+                exit;
             } else {
-                fputs($imap_stream, "A3$i APPEND \"$mailfetch_subfolder\" {" . (strlen($Message) - 1) . "}\r\n");
+                Mail_Fetch_Status(_("Mail appended to mailbox"));
             }
-            $Line = fgets($imap_stream, 1024);
-            if (substr($Line, 0, 1) == '+') {
-                fputs($imap_stream, $Message);
-                sqimap_read_data($imap_stream, "A3$i", false, $response, $message);
-                if ( $response <> 'OK' ) {
-                    Mail_Fetch_Status(_("Error Appending Message!")." ".$message );
-                    Mail_Fetch_Status(_("Closing POP"));
-                    $pop3->quit();
-                    Mail_Fetch_Status(_("Logging out from IMAP"));
-                    sqimap_logout($imap_stream);
 
-                    Mail_Fetch_Status(_("Saving UIDL"));
-                    setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
-                   exit;
+            if ($mailfetch_lmos != 'on') {
+                if( $pop3->command_dele($i) ) {
+                    Mail_Fetch_Status(sprintf(_("Mail %d deleted from remote server!"), $i));
                 } else {
-                    Mail_Fetch_Status(_("Message appended to mailbox"));
+                    Mail_Fetch_Status(_("Delete failed:") . htmlspecialchars($pop3->error) );
                 }
-                
-                if ($mailfetch_lmos != 'on') {
-                   if( $pop3->delete($i) ) {
-                        Mail_Fetch_Status(_("Message ") . $i . _(" deleted from Remote Server!"));
-                   } else {
-                        Mail_Fetch_Status(_("Delete failed:") . $pop3->ERROR );
-                   }
-                }
-            } else {
-                echo "$Line";
-                Mail_Fetch_Status(_("Error Appending Message!"));
-                Mail_Fetch_Status(_("Closing POP"));
-                $pop3->quit();
-                Mail_Fetch_Status(_("Logging out from IMAP"));
-                sqimap_logout($imap_stream);
+            }
+        } else {
+            echo $Line;
+            Mail_Fetch_Status(_("Error Appending Mail!"));
+            Mail_Fetch_Status(_("Closing POP"));
+            $pop3->command_quit();
+            Mail_Fetch_Status(_("Logging out from IMAP"));
+            sqimap_logout($imap_stream);
 
-                // not gurantee corect!
+            // not gurantee corect!
+            if ($mailfetch_lmos == 'on') {
                 Mail_Fetch_Status(_("Saving UIDL"));
-                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $mailfetch_uidl[$i-1]);
-                exit;
+                setPref($data_dir,$username,"mailfetch_uidl_$i_loop", $msglist[$i-1]);
             }
+            exit;
         }
-        
-        Mail_Fetch_Status(_("Closing POP"));
-        $pop3->quit();
-        Mail_Fetch_Status(_("Logging out from IMAP"));
-        sqimap_logout($imap_stream);
-        if (is_array($mailfetch_uidl)) {
-            Mail_Fetch_Status(_("Saving UIDL"));
-            setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($mailfetch_uidl));
-        }
-        
-        Mail_Fetch_Status(_("Done"));
+    }
+
+    Mail_Fetch_Status(_("Closing POP"));
+    $pop3->command_quit();
+    Mail_Fetch_Status(_("Logging out from IMAP"));
+    sqimap_logout($imap_stream);
+    if ($mailfetch_lmos == 'on' && is_array($msglist)) {
+        Mail_Fetch_Status(_("Saving UIDL"));
+        setPref($data_dir,$username,"mailfetch_uidl_$i_loop", array_pop($msglist));
+    }
 
-   }
+    Mail_Fetch_Status(_("Done"));
+}
 
-?>
-</center>
-</body>
-</html>
+$oTemplate->display('footer.tpl');