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="user@example.com"}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.
- ALL - return all messages matching the rest of the criteria
- ANSWERED - match messages with the \\ANSWERED flag set
- BCC "string" - match messages with "string" in the Bcc: field
- BEFORE "date" - match messages with Date: before "date"
- BODY "string" - match messages with "string" in the body of the message
- CC "string" - match messages with "string" in the Cc: field
- DELETED - match deleted messages
- FLAGGED - match messages with the \\FLAGGED (sometimes referred to as Important or Urgent) flag set
- FROM "string" - match messages with "string" in the From: field
- KEYWORD "string" - match messages with "string" as a keyword
- NEW - match new messages
- OLD - match old messages
- ON "date" - match messages with Date: matching "date"
- RECENT - match messages with the \\RECENT flag set
- SEEN - match messages that have been read (the \\SEEN flag is set)
- SINCE "date" - match messages with Date: after "date"
- SUBJECT "string" - match messages with "string" in the Subject:
- TEXT "string" - match messages with text "string"
- TO "string" - match messages with "string" in the To:
- UNANSWERED - match messages that have not been answered
- UNDELETED - match messages that are not deleted
- UNFLAGGED - match messages that are not flagged
- UNKEYWORD "string" - match messages that do not have the keyword "string"
- UNSEEN - match messages which have not been read yet
<?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:
- toaddress - full to: line, up to 1024 characters
- to - an array of objects from the To: line, with the following properties: personal, adl, mailbox, and host
- fromaddress - full from: line, up to 1024 characters
- from - an array of objects from the From: line, with the following properties: personal, adl, mailbox, and host
- ccaddress - full cc: line, up to 1024 characters
- cc - an array of objects from the Cc: line, with the following properties: personal, adl, mailbox, and host
- bccaddress - full bcc: line, up to 1024 characters
- bcc - an array of objects from the Bcc: line, with the following properties: personal, adl, mailbox, and host
- reply_toaddress - full Reply-To: line, up to 1024 characters
- reply_to - an array of objects from the Reply-To: line, with the following properties: personal, adl, mailbox, and host
- senderaddress - full sender: line, up to 1024 characters
- sender - an array of objects from the Sender: line, with the following properties: personal, adl, mailbox, and host
- return_pathaddress - full Return-Path: line, up to 1024 characters
- return_path - an array of objects from the Return-Path: line, with the following properties: personal, adl, mailbox, and host
- remail -
- date - The message date as found in its headers
- Date - Same as date
- subject - The message subject
- Subject - Same a subject
- Recent - R if recent and seen, N if recent and not seen, ' ' if not recent.
- Unseen - U if not seen AND not recent, ' ' if seen OR not seen and recent
- Flagged - F if flagged, ' ' if not flagged
- Answered - A if answered, ' ' if unanswered
- Deleted - D if deleted, ' ' if not deleted
- Draft - X if draft, ' ' if not draft
- Msgno - The message number
- Size - The message size
- udate - mail message date in Unix time
- fetchfrom - from line formatted to fit fromlength characters
- fetchsubject - subject line formatted to fit subjectlength characters
- body - the actual text from the body of the email
<?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