Search code examples
phpgmail-imap

Fatal error: Call to undefined function imap_open()


Right gentelmen, I have been at this liturary the whole day by now I'm desperate ! Here is the situation. I am using free webhosting, whenever I try to use imap_open this message comes up : Fatal error: Call to undefined function imap_open(). I obiosly dont have any access to a server setting and it would appear that php does not have the imap module installed, even though I contacted my provider (web000) with a direct question: Do you support imap functions ? He replied yes. I did send him an email with the error message, aking him how is this possible then. Havent heard from him yet. Anyway is there anyway I could access my gmail content assuming that imap functions are undefined, is there any library I could download to get this functionality ? Thanks for your time guys.


Solution

  • You can use Zend_Mail to read an imap mailbox with no need for the imap extension since it's a pure php implementation (it uses sockets to connect to a mail server) of the IMAP protocol.

    There is no special requirements to use Zend Framework on your host. Just download the package, extract it and upload on your hosting. I suggest you the minimal edition it's ok for running the following code.

    In order to connect to a Gmail account you can start with this code, it will connect to your account, get the first message stored on server and output its subject, then you can expand it.

    <?
    // Ensure Zend folder is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
        '/home/tachyon/public_html/zend/library/',
         get_include_path(),
    )));
    
    // require the ZF autoloader file if you have it in your include path
    require_once 'Zend/Loader/Autoloader.php';
    // if ZF is not in your path you can specify the full path
    // otherwise if it's in a subdir (most likely if you're on a web hosting)
    // you can do something like this
    //require_once dirname(__FILE__) . '/Zend/Loader/AutoLoader.php';
    
    // laod the autoloader so you don't need to require any ZF file
    Zend_Loader_Autoloader::getInstance();
    
    // connecting with Imap to gmail
    $mail = new Zend_Mail_Storage_Imap(
        array(
            'host'     => 'imap.gmail.com',
            'port'     => '993',
            'ssl'      => true,
            'user'     => 'user@gmail.com',
            'password' => 'secret',
        )
    );
    
    // get the message object
    $message = $mail->getMessage(1);
    // output subject of message
    echo $message->subject . "\n";
    // dump message headers
    Zend_Debug::dump($message->getHeaders());
    

    I personally tested this with my Gmail account, so it's working code.