Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by sophistry )

IMAP

IMAP functionality in CodeIgniter

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:
http://codeigniter.com/wiki/imap_pop_class/

<?php

    
/* Controller: Mail */

    
class Mail extends Controller {
        

        
// IMAP/POP3 (mail server) LOGIN
        
var $imap_server    = 'mail.example.org';
        var
$imap_user        = 'user@example.org';
        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);    
        
}
    }

?>

<?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) . '%)';
        
}    

    }

?>


/* mail_view */

Will return something like:

Total messages: 13

Quota: 8.27 Mb / 10 Mb (82.67%)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>

<
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="" />

    <
title>Title</title>
    
</
head>

<
body>

    <
p>Total messages: <?=$totalmsg?></p>
    <
p>Quota: <?=$quota?></p>

</
body>
</
html>

Category:Libraries -> Email

Categories: