diff --git a/include/class.config.php b/include/class.config.php
index 95a5204e389b55c161cf192c1af9511793732e70..e4183779f4db2da35eea349b252512df97af2baf 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -313,7 +313,7 @@ class Config {
     }
         
     function canFetchMail() {
-        return ($this->config['enable_mail_fetch']);
+        return ($this->config['enable_mail_polling']);
     }
 
     function enableStaffIPBinding() {
diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 4ee15ecca38404efcd943fcd984d6b3d335854b6..f25d72b26236471dd6b14e365a1745067ffe6f3e 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -346,7 +346,7 @@ class MailFetcher {
 
     }
 
-    function fetchTickets($emailid,$max=20,$deletemsgs=false){
+    function fetchTickets($emailid,$max=20,$deletemsgs=false,$archivefolder){
 
         $nummsgs=imap_num_msg($this->mbox);
         //echo "New Emails:  $nummsgs\n";
@@ -354,7 +354,7 @@ class MailFetcher {
         for($i=$nummsgs; $i>0; $i--){ //process messages in reverse. Latest first. FILO.
             if($this->createTicket($i,$emailid)){
                 imap_setflag_full($this->mbox, imap_uid($this->mbox,$i), "\\Seen", ST_UID); //IMAP only??
-                if($deletemsgs)
+                if((!$archivefolder || !imap_mail_move($this->mbox,$i,$archivefolder)) && $deletemsgs)
                     imap_delete($this->mbox,$i);
                 $msgs++;
                 $errors=0; //We are only interested in consecutive errors.
@@ -384,7 +384,7 @@ class MailFetcher {
 
         $MAX_ERRORS=5; //Max errors before we start delayed fetch attempts - hardcoded for now.
 
-        $sql=' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_errors,userid,userpass FROM '.EMAIL_TABLE.
+        $sql=' SELECT email_id,mail_host,mail_port,mail_protocol,mail_encryption,mail_delete,mail_archivefolder,mail_errors,userid,userpass FROM '.EMAIL_TABLE.
              ' WHERE mail_active=1 AND (mail_errors<='.$MAX_ERRORS.' OR (TIME_TO_SEC(TIMEDIFF(NOW(),mail_lasterror))>5*60) )'.
              ' AND (mail_lastfetch IS NULL OR TIME_TO_SEC(TIMEDIFF(NOW(),mail_lastfetch))>mail_fetchfreq*60) ';
         //echo $sql;
@@ -396,7 +396,7 @@ class MailFetcher {
             $fetcher = new MailFetcher($row['userid'],Misc::decrypt($row['userpass'],SECRET_SALT),
                                        $row['mail_host'],$row['mail_port'],$row['mail_protocol'],$row['mail_encryption']);
             if($fetcher->connect()){   
-                $fetcher->fetchTickets($row['email_id'],$row['mail_fetchmax'],$row['mail_delete']?true:false);
+                $fetcher->fetchTickets($row['email_id'],$row['mail_fetchmax'],$row['mail_delete']?true:false,$row['mail_archivefolder']);
                 $fetcher->close();
                 db_query('UPDATE '.EMAIL_TABLE.' SET mail_errors=0, mail_lastfetch=NOW() WHERE email_id='.db_input($row['email_id']));
             }else{
diff --git a/include/class.ticket.php b/include/class.ticket.php
index c1e4cdf23803dd48065dadef0da032130b1283dd..ffee446b17e0c300ee0d15bcf406a010f7dfdb46 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1310,7 +1310,7 @@ class Ticket{
 
         if ($emsgid !== null) {
             $sql='INSERT INTO '.TICKET_EMAIL_INFO_TABLE
-                .' SET msg_id='.db_input($msgid)
+                .' SET message_id='.db_input($msgid)
                 .', email_mid='.db_input($emsgid)
                 .', headers='.db_input($headers);
 
@@ -1719,7 +1719,7 @@ class Ticket{
             return 0;
 
         $sql='SELECT ticket.ticket_id FROM '.TICKET_TABLE. ' ticket '.
-             ' LEFT JOIN '.TICKE_THREAD_TABLE.' msg USING(ticket_id) '.
+             ' LEFT JOIN '.TICKET_THREAD_TABLE.' msg USING(ticket_id) '.
              ' INNER JOIN '.TICKET_EMAIL_INFO_TABLE.' emsg ON (msg.id = emsg.message_id) '.
              ' WHERE email_mid='.db_input($mid).' AND email='.db_input($email);
         $id=0;
diff --git a/main.inc.php b/main.inc.php
index 7f8c2ba3e6ffa02eb499bc5f82ba1ca85e7fef31..fa1ca5da9c0745487af603b49fbcde740c09c61f 100644
--- a/main.inc.php
+++ b/main.inc.php
@@ -141,6 +141,7 @@
     define('PRIORITY_TABLE',TICKET_PRIORITY_TABLE);
     define('TICKET_LOCK_TABLE',TABLE_PREFIX.'ticket_lock');
     define('TICKET_EVENT_TABLE',TABLE_PREFIX.'ticket_event');
+    define('TICKET_EMAIL_INFO_TABLE',TABLE_PREFIX.'ticket_email_info');
   
     define('EMAIL_TABLE',TABLE_PREFIX.'email');
     define('EMAIL_TEMPLATE_TABLE',TABLE_PREFIX.'email_template');