Hudzilla.org - the homepage of Paul Hudson
Contents > Networks > Reading mail Wish List | Report Bug | About Me ]

15.6.1     Opening a mailbox: imap_open() and imap_close()

This is NOT the latest copy of this book; click here for the latest version.

resource imap_open ( string mailbox, string username, string password [, int options])

bool imap_close ( resource imap_stream [, int flag])

The first step to reading email is learning how to open and close the connection. As you should have guessed by now, this is accomplished through the imap_open() and imap_close() functions. The first of these two takes a mail box to open as its first parameter, then a username and password as parameters two and three respectively. The first parameter is where the odd syntax comes in - here is an example of imap_open() in action:

<?php
    
print imap_open("{mail.yourserver.com:143}INBOX", "username", "password");
?>

Running that script, once you have changed the domain name, username, and password to valid values, should print out something like "Resource id #4". If you get nothing printed out, it means the connection was unsuccessful and you probably need to check you have it typed in correctly.

Once you get your script working, you should see that the imap_open() function returns a resource for the IMAP connection - this is technically known as the IMAP stream. This should be stored for later use, so place it into a variable such as $imap.

Now, the first parameter to imap_open() is where the confusion might come in. In the example above, it designates a connection to the server mail.yourserver.com on port 143, then selects the folder INBOX. The braces, { and }, are required, and should not include any spaces inside them. Some IMAP servers have different forms for connection. For example, connection to Microsoft Exchange might require you to use "{mail.yourserver.com:143/imap}Inbox" and also provide the username as "<yourdomain>/<yourusername>". Also, if you are running your IMAP server using SSL, you may need to use "{mail.yourserver.com:993/ssl/novalidate-cert}INBOX". I recommend you imap_open() as shown in the code example above unless you specifically need to change it.

Once you are finished with the connection, call imap_close() and pass the IMAP connection resource variable as its only parameter, like this:

<?php
    $imap
= imap_open("{mail.yourserver.com:143}INBOX", "username", "password");
    
imap_close($imap);
?>

Being able to connect to and disconnect from a mail server is not very impressive, but it is a good start - read on!

Author's Note: It is best to specify the server by its IP address (such as 212.192.219.29) as opposed to the domain name, as you will likely find it a great deal faster. For the purpose of this book domain names are used as they make for much easier reading.





<< 15.6 Reading mail   15.6.2 Reading message information: imap_headers() and imap_header() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus three?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow