CSS: Providing seven essential vitamins and minerals since 1996.
Have a steaming hot, delicious slice of Standards.

Mail_IMAP - A Webmail API for PHP

  • Home
  • ->Mail_IMAP - A Webmail API for PHP
December 31, 1969 by richard

Donate: If you have found Mail_IMAP to be helpful in your project please express your appreciation by donating to my efforts through my Amazon wishlist.

Mail_IMAP provides a flexible API for connecting to and retrieving mail from mailboxes using the IMAP, POP3 or NNTP mail protocols. Connection to a mailbox is acheived through the c-client extension to PHP. Meaning installation of the c-client extension is required to use Mail_IMAP.

Mail_IMAP can be used to retrieve the contents of a mailbox, whereas it may serve as the backend for a webmail application or mailing list manager.

Since Mail_IMAP is an abstracted object, it allows for complete customization of the UI for any application.

By default Mail_IMAP parses and retrieves information about multipart messages in a threaded fashion similar to MS Outlook, e.g. only top level attachments are retrieved initially, then when message part id and message id are passed to Mail_IMAP, it retrieves attachments and information relevant to that message part. Mail_IMAP::getParts can be supplied an argument to turn off threading, whereas all parts are retrieved at once.

Mail_IMAP also, by default retrieves the alternative message parts of multipart messages. This is most useful for debugging applications that send out multipart mailers where both a text/html and alterntaive text/plain part are included. This can also be turned off by supplying an additional argument to Mail_IMAP::getParts.

Mail_IMAP always searches for a text/html part to display as the primary part. This can be reversed so that it always looks for a text/plain part initially by supplying the necessary arguments to Mail_IMAP::getParts, and Mail_IMAP::getBody.

Note: The Mail_IMAP package name for Mail_IMAP 2 has become Mail_IMAPv2. This was done to follow PEAR naming conventions and to avoid possible conflicts with the IMAP2 protocol. There may be several pages referring to the old package name, Mail_IMAP, in the documentation.

Mail_IMAP 2 Documentation

Warning: Although I have documented Mail_IMAP's private and protected properties and methods, these should be used with caution (if used at all). The API of these properties can change without notice, since I consider this internal functionality of Mail_IMAP. I will tweak these if I decide there's a better way of doing things! Furthermore, access to these methods will be enforced under the upcoming PHP 5 E_STRICT port of Mail_IMAP.

Comments

  • Posted: November 4, 2008 Jacob Edward says:

    Hello,

    Could kindly post an example of how to use this library.
    Its looks very useful.
  • Posted: February 12, 2009 Eric Dorsey says:

    I am working with your Mail_IMAP and Mail_IMAPv2 PEAR modules and I cannot get them to work. I noticed the documentation is extremely old and I cannot for the life of me understand what the problem is. I have used all of your examples with both versions (IMAP.php and IMAPv2.php).

    When I use the 'IMAP.connection_wizard_example.php' with 'IMAP.php' it returns 'A suitable URI could not be detected.' when I use the same file with 'IMAPv2.php' it returns nothing.

    When I try the 'IMAP.index.php' with IMAP.php is returns 'Unable to build a connection.' and when I try it with IMAPv2.php it still fails and throws an Error having to with 'PEAR_Error'.

    I am running on an Ubuntu8.10 Server with apache2, php5 and pear1.1.1. It is a brand new install.

    Thanks for any information you can provide,

    Eric Dorsey
  • Posted: March 19, 2009 spaze says:

    So I use it like this (extra PEAR packages required, check the comments), the code is pretty self-explanatory, but in short, it displays total count of messages on IMAP server, reads the first, displays some headers and body and then deletes the message.

    $mbox = new Mail_IMAPv2();
    $uri = 'imap://username:password@imapmailserver.example.com:143/INBOX';
    // Net_URL package is required when using connect() method
    $mbox->connect($uri);
    echo $mbox->messageCount();

    $params['include_bodies'] = TRUE;
    $params['decode_bodies'] = TRUE;
    $number = 1; // message number one
    $raw_message = $mbox->getRawHeaders($number).$mbox->getRawMessage($number);
    $params['input'] = $raw_message;

    // Mail_mimeDecode package required for parsing messages
    $structure = Mail_mimeDecode::decode($params);

    echo $structure->headers['message-id'];
    echo $structure->headers['date'];
    echo $structure->body;

    $mbox->delete($number); // Only marks for deletion
    $mbox->expunge(); // actual deletion
    $mbox->close();

Leave a Comment

Copyright © 1998-2009 Richard York, All Rights Reserved.
PHP Powered XHTML 1.0 CSS
Execution time: 0.07 seconds