Wrox Programmer Forums Q. How do I access my email with PHP?
 |
 PHP FAQs This moderated forum is where our PHP experts answer some of the questions they see asked most frequently in the other PHP forums. You cannot post other messages here, use the other PHP forums for that.
 Welcome to the p2p.wrox.com Forums. You are currently viewing the PHP FAQs section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com

April 8th, 2004, 09:51 PM
 richard.york Wrox Author Join Date: Jun 2003 Posts: 1,706 Thanks: 0 Thanked 6 Times in 6 Posts
Q. How do I access my email with PHP?

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 p2p 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. I personally spent the better part of six months exploring and developing a mail client of my own. 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:
Code:
    pear install Mail_IMAP
If the command line refuses to install the file because a stable version doesn't exist (beta released as of this writing), download it and install it via the file path to the downloaded tarball.
Code:
    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:
Code:
    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):
Code:
    require_once 'Mail/IMAP.php';
Review the documentation files located at:
Code:
    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

 Similar Threads Thread Thread Starter Forum Replies Last Post PHP Email aftabn10 PHP How-To 2 January 30th, 2007 05:43 AM email php sunsetbay Beginning PHP 7 July 16th, 2005 01:57 PM Email In php 4 philuk86 Beginning PHP 19 July 14th, 2004 05:31 PM trouble with Email and PHP Tachyon Beginning PHP 3 May 26th, 2004 03:58 AM php recordset and email cindy Beginning PHP 1 September 22nd, 2003 12:59 PM