Home > PHP > POP3/IMAP library for CodeIgniter

POP3/IMAP library for CodeIgniter

I was looking for POP3/IMAP library to read and delete emails from POP3/IMAP accounts but one I found on CodeIgniter forums was quite buggy. Fortunately I noticed this post on Reddit which featured Flourish PHP “unframework” and it’s fMailbox class.

I tried to run fMailbox class stand-alone and it worked quite well, so I decided to use it on my CodeIgniter application. I had to change how this class is initiated to use it as regular CodeIgniter library. Also in some places I had to replace Flourish exceptions with CodeIgniter’s error handling.

So, this is how you initiate library with just required parameters:

1
2
3
4
5
6
$config['type'] = 'imap';
$config['host'] = 'mail.example.com';
$config['username'] = 'info@example.com';
$config['password'] = 'letsplay';
 
$this->load->library('mailbox', $config);

More options are available:

1
2
3
4
5
6
7
8
9
$config['type'] = 'imap';
$config['host'] = 'mail.example.com';
$config['username'] = 'info@example.com';
$config['password'] = 'letsplay';
$config['port'] = 993;
$config['secure'] = TRUE;
$config['timeout'] = 5;
 
$this->load->library('mailbox', $config);

And this is how you call methods:

1
$messages = $this->mailbox->listMessages(10);

So it works basically as documented here, but you have to pass initial arguments as array. Hope someone will find it useful, here’s the download, just copy Mailbox.php to your “libraries” directory and you should be good to go.

Tags:
  1. September 5th, 2010 at 09:20 | #1

    Wonderful tip !

    Thanks,
    Alexis Serneels

  2. gabriel
    March 10th, 2011 at 04:03 | #2

    In initialize() you seem to have an undefined private variable $this->key.

    foreach ($defaults as $key => $val)
    {
    if (isset($config[$key]))
    {
    $this->$key = $config[$key];
    }
    else
    {
    $this->$key = $val;
    }
    }

  3. gabriel
    March 10th, 2011 at 04:17 | #3

    @gabriel

    Sorry please ignore the comment

  4. March 10th, 2011 at 04:18 | #4

    Hi Gabriel, there is no call to $this->key, if you are referring to $this->$key, it is converted in the loop to $this->type, $this->host, etc.

  5. Siregar
    June 20th, 2011 at 21:30 | #5

    I got this error.

    A PHP Error was encountered

    Severity: Warning

    Message: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known.

    Filename: libraries/Mailbox.php

    Line Number: 871
    A PHP Error was encountered

    Severity: Warning

    Message: fsockopen() [function.fsockopen]: unable to connect to stmp.gmail.com:465 (php_network_getaddresses: getaddrinfo failed: No such host is known. )

    Filename: libraries/Mailbox.php

    Line Number: 871
    A PHP Error was encountered

    Severity: Warning

    Message: Cannot modify header information – headers already sent by (output started at D:\Siregar\Study\Sem IV\IBAD\KP\CodeIgniter_2.0.2\system\core\Exceptions.php:170)

    Filename: core/Common.php

    Line Number: 413
    An Error Was Encountered

    Unable to send data since the connection has already been closed

    and my configuration is like this..,
    $config['type'] = ‘imap’;
    $config['host'] = ‘stmp.gmail.com’;
    $config['username'] = ‘myemail@gmail.com’;
    $config['password'] = ‘mypwd’;
    $config['port'] = 465;
    $config['secure'] = false;
    $config['timeout'] = 5;

    $this->load->library(‘mailbox’, $config);
    $messages = $this->mailbox->listMessages(10);

    what wrong, why mine didn’t work well.,thank you for your help.,

  6. June 21st, 2011 at 04:49 | #6

    This is a problem with your server configuration, but it can be caused by a few different issues. Search for “fsockopen() unable to connect” and you’ll find plenty of advice.

  7. Siregar
    June 21st, 2011 at 20:08 | #7

    thank you for solution, it work well.

    But, i didn’t get the attachment

    the result is like this

    Array
    (
    [uid] => 4
    [received] => 22 Jun 2011 00:34:19 +0000
    [size] => 88988
    [date] => Wed, 22 Jun 2011 08:34:19 +0800
    [from] => “Sender”
    [subject] => Coba
    [message_id] =>
    [to] => receiver@gmail.com
    )

  1. October 1st, 2010 at 13:44 | #1
  2. October 1st, 2010 at 13:45 | #2