Index: index.php
===================================================================
--- index.php	(revision 9b94eb64153a7dc6555d6b9a30a35296ce592f82)
+++ index.php	(revision 48bc52e835bd5485f7443d54399e4fb0d36732d7)
@@ -31,12 +31,6 @@
 require_once 'program/include/iniset.php';
 
-// init application and start session with requested task
+// init application, start session, init output class, etc.
 $RCMAIL = rcmail::get_instance();
-
-// init output class
-$OUTPUT = !empty($_REQUEST['_remote']) ? $RCMAIL->init_json() : $RCMAIL->load_gui(!empty($_REQUEST['_framed']));
-
-// init plugin API
-$RCMAIL->plugins->init();
 
 // turn on output buffering
Index: program/include/rcmail.php
===================================================================
--- program/include/rcmail.php	(revision 9b94eb64153a7dc6555d6b9a30a35296ce592f82)
+++ program/include/rcmail.php	(revision 48bc52e835bd5485f7443d54399e4fb0d36732d7)
@@ -131,10 +131,15 @@
     $_SESSION['task'] = $this->task;
 
-    // create IMAP object
-    if ($this->task == 'login')
-      $this->imap_init();
-      
+    // init output class
+    if (!empty($_REQUEST['_remote']))
+      $GLOBALS['OUTPUT'] = $this->init_json();
+    else
+      $GLOBALS['OUTPUT'] = $this->load_gui(!empty($_REQUEST['_framed']));
+
     // create plugin API and load plugins
     $this->plugins = rcube_plugin_api::get_instance();
+
+    // init plugins
+    $this->plugins->init();
   }
   
@@ -396,5 +401,9 @@
     // set global object for backward compatibility
     $GLOBALS['IMAP'] = $this->imap;
-    
+
+    $hook = $this->plugins->exec_hook('imap_init', array('fetch_headers' => $this->imap->fetch_add_headers));
+    if ($hook['fetch_headers'])
+      $this->imap->fetch_add_headers = $hook['fetch_headers'];
+				        
     if ($connect)
       $this->imap_connect();
@@ -410,4 +419,7 @@
   {
     $conn = false;
+
+    if (!$this->imap)
+      $this->imap_init();
     
     if ($_SESSION['imap_host'] && !$this->imap->conn) {
@@ -494,4 +506,7 @@
     if ($user = rcube_user::query($username, $host))
       $username = $user->data['username'];
+
+    if (!$this->imap)
+      $this->imap_init();
 
     // exit if IMAP login failed
Index: program/include/rcube_plugin_api.php
===================================================================
--- program/include/rcube_plugin_api.php	(revision 9b94eb64153a7dc6555d6b9a30a35296ce592f82)
+++ program/include/rcube_plugin_api.php	(revision 48bc52e835bd5485f7443d54399e4fb0d36732d7)
@@ -151,13 +151,4 @@
     
     // maybe also register a shudown function which triggers shutdown functions of all plugin objects
-    
-    
-    // call imap_init right now
-    // (should actually be done in rcmail::imap_init() but plugins are not initialized then)
-    if ($rcmail->imap) {
-      $hook = $this->exec_hook('imap_init', array('fetch_headers' => $rcmail->imap->fetch_add_headers));
-      if ($hook['fetch_headers'])
-        $rcmail->imap->fetch_add_headers = $hook['fetch_headers'];
-    }
   }
   
Index: program/steps/mail/func.inc
===================================================================
--- program/steps/mail/func.inc	(revision 91790e41f3fa307658077043bc2fa5f71e270cf4)
+++ program/steps/mail/func.inc	(revision 48bc52e835bd5485f7443d54399e4fb0d36732d7)
@@ -25,7 +25,4 @@
 $NOIMAP_ACTIONS = array('spell', 'addcontact', 'autocomplete', 'upload', 'display-attachment', 'remove-attachment');
 
-// Init IMAP object
-$RCMAIL->imap_init();
-
 // log in to imap server
 if (!in_array($RCMAIL->action, $NOIMAP_ACTIONS) && !$RCMAIL->imap_connect()) {
@@ -43,5 +40,5 @@
 if ($mbox = get_input_value('_mbox', RCUBE_INPUT_GPC))
   $IMAP->set_mailbox(($_SESSION['mbox'] = $mbox));
-else
+else if ($IMAP)
   $_SESSION['mbox'] = $IMAP->get_mailbox_name();
 
