Get mail info with IMAP or POP3

To read mail using IMAP or POP3, which allows you to create a web-based email client.

Use PHP's IMAP extension, which speaks both IMAP and POP3:

// open IMAP connection
$mail = imap_open('{}',      'username', 'password');
// or, open POP3 connection
$mail = imap_open('{}', 'username', 'password');

// grab a list of all the mail headers
$headers = imap_headers($mail);

// grab a header object for the last message in the mailbox
$last = imap_num_msg($mail);
$header = imap_header($mail, $last);

// grab the body for the same message
$body = imap_body($mail, $last);

// close the connection

The underlying library PHP uses to support IMAP and POP3 offers a seemingly unending number
 of features that allow you to essentially write an entire mail client. With all those features,
 however, comes complexity. In fact, there are currently 63 different functions in PHP beginning
with the word imap, and that doesn't take into account that some also speak POP3 and NNTP.

However, the basics of talking with a mail server are straightforward. Like many features in PHP,
 you begin by opening the connection and grabbing a handle:

$mail = imap_open('{}', 'username', 'password');
This opens an IMAP connection to the server named on port 143.
It also passes along a username and password as the second and third arguments.

To open a POP3 connection instead, append /pop3 to the end of the server and port.
 Since POP3 usually runs on port 110, add :110 after the server name:

$mail = imap_open('{}', 'username', 'password');

To encrypt your connection with SSL, add /ssl on to the end, just as you did with pop3.
 You also need to make sure your PHP installation is built with the --with-imap-ssl
configuration option in addition to --with-imap. Also, you need to build the system IMAP
library itself with SSL support. If you're using a self-signed certificate and wish to
prevent an attempted validation, also add /novalidate-cert. Finally, most SSL connections talk
on either port 993 or 995. All these options can come in any order, so the following is perfectly legal.

$mail = imap_open('{}',
                  'username', 'password');
Surrounding a variable with curly braces inside of a double-quoted string, such as {$var},
 is a way to tell PHP exactly which variable to interpolate. Therefore, to use interpolated variables
 in this first parameter to imap_open( ), escape the opening 

$server = '';
$port = 993;

$mail = imap_open("\{$server:$port}", 'username', 'password');

Once you've opened a connection, you can ask the mail server a variety of questions.
 To get a listing of all the messages in your inbox, use imap_headers( ):

$headers = imap_headers($mail);
This returns an array in which each element is a formatted string corresponding to a message:

Alternatively, to retrieve a specific message, use imap_header( ) and imap_body( ) to pull
the header object and body string:

$header = imap_header($message_number);
$body   = imap_body($message_number);
The imap_header( ) function returns an object with many fields. Useful ones include subject,
 fromaddress, and udate.