PDA

View Full Version : [FAQ] How do I access my email with PHP?



mdshare
04-07-2008, 09:37 PM
Faq by Richard Heyes

PHP, as it turns out offers quite powerful options for connecting to mailboxes. As with most things, there is more than one option. However to go through all of the code required for such an undertaking is beyond the scope of the CE forums.

However, abstractly speaking here are a few options for email access with PHP..

1.) Connect to the mail server via sockets.

This method requires a raw command line interface taylored to the mail protocol. The most common protocols being POP3, or (P)ost (O)ffice (P)rotocol, version 3, and IMAP (I)nternet (M)essage (A)ccess (P)rotocol.

The business of connecting to a mailbox requires a great deal of planning and development time. The simple solution to this problem is to use already built applications and taylor them to your specific needs. PHP PEAR (P)HP (E)xtension and (A)pplication (R)epository offers serveral already built solutions for this method. Though at the time of this writing, none preform *all* of the backend work required for a PHP socket-based connection. This solution would still require a great deal of development time.

Currently PEAR provides the following PHP socket-based solutions:
Net_IMAP developed by Damian Alejandro Fernandez Sosa
Net_POP3 developed by Richard Heyes (the MIME mail guru)

Richard Heyes' Mail_MIME package also has a multipart message parser in addition to the multipart message builder.

2.) Connect to the mail server using the PHP c-client (imap) extension.

This method relies on a set of functions that are available in the PHP imap extension. The imap name is a little misleading in that these functions support connection to more than just the imap protocol, those being: POP3, IMAP and NNTP. The advantages to using the c-client extension as opposed to PHP socket-based connections are tremendous, in terms of memory consumption, and overall speediness. Put simply, PHP sockets are slow and cumbersome, c-client is written in C, and consequently this makes it much faster.

The imap extension is an extended feature of PHP, meaning that you have to either compile PHP with the extension or, in Windows, download the .dll and uncomment the extension reference in php.ini. The .dll file is available in the .zip download of PHP for Windows, for Linux see the imap manual page for instructions.

After I had developed my own webmail application based on the imap extension, I decided to abstract and release it in a PEAR package, Mail_IMAP. This package simplifies the webmail backend and does all the dirty work involved. It allows for a completely customizable aesthetic design and webmail user inferface. It tells you what is in a message, handles attachments, and provides information about the mailbox. It can even detect the user's protocol and port setting, based on nothing more than the server location, the user name and password. (Not to toot my own horn or anything)

Mail_IMAP package page

How to install Mail_IMAP
Mail_IMAP is installable via the PEAR interface.

At the command line, cd to the PHP directory (where the pear.bat file is located)

Enter:


pear install Mail_IMAP

If the command line refuses to install the file because a stable version doesn't exist , download it and install it via the file path to the downloaded tarball.


pear install C:\path\to\tarball\Mail_IMAP-1.0.0RC3.tgz

Verify the installation by going to the PHP install directory.

The file should be located here, or wherever PEAR is installed:

C:\PHP\PEAR\Mail\IMAP.php

Now the class file is available via the base include path.
See the php.ini directive include_path (this should have a value of C:\PHP\PEAR\ or wherever PHP and PEAR are installed.)

The class may now be included via the following from any script in any directory (via the include_path directive):



require_once 'Mail/IMAP.php';

Review the documentation files located at:

C:\PHP\PEAR\docs\Mail_IMAP\examples

The documentation files include a demonstration inbox and message viewer.

Extended documentation is available at:
http://www.smilingsouls.net/Mail_IMAP

Other pre-built PHP mail solutions also exist.

One being Horde:
http://www.horde.org

Another being Squirrel Mail:
http://www.squirrelmail.org