Imap Library User Guide - 377 Downloads and Counting!

The Imap library is extremely easy to use!

Download Imap

Installation

Copy application/libraries/imap.php to your application/libraries folder. Copy application/config/imap.php to your application/config folder.

You can set any variables without using the imap.php config file.

Troubleshooting

Have a question that isn't answered here? Follow this library's forum on CodeIgniter.com!

connect()

Connects to your imap server. If you don't want to use the config file, you can connect with by including the username, password, and mailbox as parameters. On error, this function will return the error string. <?php
$this
->load->library('imap');

$username 'bill.gates';
$password 'ihateapple';
$mailbox 'mail.microsoft.com';

if(
$this->imap->connect($username$password$mailbox))
{
    echo 
'I\'m connected!';
}

current_mailbox()

Returns the name of the current mailbox. If there's an error, this function will return that error. <?php
$current_mailbox 
$this->imap->current_mailbox();

echo 
$current_mailbox;    // returns {mail.example.com:143/imap/notls/user=&quot;user@example.com&quot;}INBOX

msg_count()

Gets the number of messages in the current mailbox. If there's an error, this function will return that error. <?php
$msg_count 
$this->imap->msg_count();

echo 
$msg_count;    // returns an integer

delete()

Deletes a message by it's message number. Takes an integer or an array of message ids as the first parameter. If you would prefer messages to be permanently removed from the server, give TRUE as the second parameter. <?php
$msgs_to_delete 
= array(3,5,12,55);

if(
$this->imap->delete($msgs_to_delete))
{
    echo 
'Messages deleted!';
}

mailbox_info()

Returns information about the current mailbox in an object. If you would rather return an array, use 'array' as the first parameter. The following information is returned:

Date date of last change (current datetime)
Driver driver
Mailbox name of the mailbox
Nmsgs number of messages
Recent number of recent messages
Unread number of unread messages
Deleted number of deleted messages
Size mailbox size
<?php
$array 
$this->imap->mailbox_info('array');
$obj $this->imap->mailbox_info();

echo 
'There are ' $array['Unread'] . ' unread messages in ' $obj->Mailbox '...';
echo 
br();
echo 
'But there are ' $obj->Deleted ' deleted messages in ' $array['Mailbox'] . '...';

switch_mailbox()

Switches the current stream to a new mailbox, ie. Trash, Sent, etc. If no first parameter is given, the stream will return to the default mailbox. <?php
$this
->imap->switch_mailbox('Trash');

search()

Returns an array of the message numbers of messages that match the specified criteria. Takes an array as the required parameter. The array can be both associative and linear. Indexes aren't case sensitive.

 

<?php
$search
[] = 'UNSEEN';
$search['TO'] = 'bill.gates@microsoft.com';
$search['SUBJECT'] = 'apple';

$msgs $this->imap->search($search);

print_r($msgs);

msg_list()

This returns an array with information on each message. Takes a single message number or an array of messages as the first parameter. If no parameter is given, this function will return all the messages in the current mailbox. The following indexes are available in the returned array:

 

<?php
$search
[] = 'UNSEEN';
$search['TO'] = 'bill.gates@microsoft.com';
$search['SUBJECT'] = 'apple';

$msgs $this->imap->search($search);

$msg_info $this->imap->msg_list($msgs);
print_r($msg_info);

Configuration

Configuration variables are included in application/config/imap.php.

$config['imap_user'] - Your account username

$config['imap_pass'] - Your account password

$config['imap_server'] - The server you want to connect to ie. mail.example.com

$config['imap_port'] - Specify the port you want to use (SSL is 143)

$config['imap_flags'] - Optional flags (click here for more info)

$config['imap_mailbox'] - Server and a mailbox path

Download Imap Library