58
683
Reading Mail
For example,to read mail from the local machine using the default ports,you
would use the following mailbox name for IMAP:
{localhost:143}INBOX
And you would use this one for POP3:
{localhost/pop3:110}INBOX
n
username
—The username for the account.
n
password
—The password for the account.
You can also pass it optional flags to specify options such as
“open mailbox in read-
only mode”
.
Note that we constructed the mailbox string piece by piece with the concatenation
operator before passing it to
imap_open()
.You need to be careful how you construct
this string because strings containing
{$
can cause problems in PHP.
This function call returns an IMAP stream if the mailbox can be opened and
false
if
it cannot.
When you are finished with an IMAP stream,you can close it by using
imap_close(imap_stream)
.In this function,the IMAP stream is passed back to the
main program.You then use the
imap_headers()
function to get the email headers for
display:
$headers = imap_headers($imap);
This function returns header information for all mail messages in the mailbox you have
connected to.The information is returned as an array, one line per message.This infor-
mation has not been formatted.The function just outputs one line per message,so you
can see from looking at Figure 29.5 what the output looks like.
You can get more information about email headers using the confusing, similarly
named
imap_header()
function. In this case,though,the
imap_headers()
function
gives you enough detail for the purposes of this project.
Reading a Mail Message
Each message in the previous
display_list()
function is set up to link to specific
email messages.Each link is of the form
index.php?action=view-message&messageid=6
The
messageid
is the sequence number used in the headers retrieved earlier.Note that
IMAP messages are numbered from 1,not 0.
If the user clicks one of these links,he will see output like that shown in Figure 29.6.