Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 19 revisions

[b]IMAP functionality in CodeIgniter[/b]

The point of this article is to develop some kind of IMAP functionality to CI, to connect to a mail server through IMAP, POP3 or NNTP.

Also to make a Class with the most used functions in building a webmail service.

EDIT... (by sophistry) A full wrapper class can be found at this page: [url=http://codeigniter.com/wiki/IMAP_POP3_Wrapper_Class/]http://codeigniter.com/wiki/IMAP_POP3_Wrapper_Class/[/url]

[code]

<?php

/* Controller: Mail */

class Mail extends Controller {
    

    // IMAP/POP3 (mail server) LOGIN
    var $imap_server    = 'mail.example.org';
    var $imap_user        = '[email protected]';
    var $imap_pass        = 'password';


    // Constuctor
    
    function __construct() {

        parent::Controller();
        
        $this->load->library('Imap');
                    
    }

    // index
    
    function index() {
                
        $inbox = $this->imap->cimap_open($this->imap_server, 'INBOX', $this->imap_user, $this->imap_pass) or die(imap_last_error());

        $data_array['totalmsg']    = $this->imap->cimap_num_msg($inbox);
        $data_array['quota']    = $this->imap->cimap_get_quota($inbox);
        
        $this->load->view('mail_view', $data_array);    
    }
}

?> [/code]

[code]

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/* Library Class: Imap */

class Imap {
    

    // Open IMAP connection
    
    function cimap_open($host, $mailbox, $username, $password)
    {
        return imap_open('{'.$host.':143}'.$mailbox, $username, $password);
    }
    
    // Find number of msg in mailbox
    
    function cimap_num_msg($imap_connection)
    {
        return imap_num_msg($imap_connection);
    }
    
    // Find disk quota amount
    
    function cimap_get_quota($imap_connection)
    {
        $storage = $quota['STORAGE']= imap_get_quotaroot($imap_connection, "INBOX");
        
        function kilobyte($filesize)
        {
            return round($filesize / 1024, 2) . ' Mb';
        }
        
        return kilobyte($storage['usage']) . ' / ' . kilobyte($storage['limit']) . ' (' . round($storage['usage'] / $storage['limit'] * 100, 2) . '%)';
    }    

}

?>

[/code]

/* mail_view */

Will return something like:

Total messages: 13

Quota: 8.27 Mb / 10 Mb (82.67%)

[code]

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="no" lang="no"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="robots" content="index,follow" /> <meta name="description" content="" /> <meta name="keywords" content="" />

&lt;title&gt;Title&lt;/title&gt;

</head>

<body>

<p>Total messages: &lt;?=$totalmsg?&gt;</p>
<p>Quota: &lt;?=$quota?&gt;</p>

</body> </html>

[/code]

Clone this wiki locally