PHP Manual
Prev |  | Next

# imap_open

imap_open -- Open an IMAP stream to a mailbox

## Description

`int imap_open`(string mailbox, string username, string password, int [flags] );

Returns an IMAP stream on success and false on error. This function can also be used to open streams to POP3 and NNTP servers. To connect to an IMAP server running on port 143 on the local machine, do the following:

      1
      2 $mbox = imap_open ("{localhost:143}INBOX", "user_id", "password");
      3

To connect to a POP3 server on port 110 on the local server, use:

      1
      2 $mbox = imap_open ("{localhost/pop3:110}INBOX", "user_id", "password");
      3

To connect to an NNTP server on port 119 on the local server, use:

      1
      2 $nntp = imap_open ("{localhost/nntp:119}comp.test", "", "");
      3

To connect to a remote server replace "localhost" with the name or the IP address of the server you want to connect to.

The options are a bit mask with one or more of the following:

- OP_READONLY - Open mailbox read-only

- OP_ANONYMOUS - Dont use or update a `.newsrc` for news (NNTP only)

- OP_HALFOPEN - For IMAP and NNTP names, open a connection but dont open a mailbox

- CL_EXPUNGE - Expunge mailbox automatically upon mailbox close

Example 1. imap_open() example
      1
      2 $mbox = imap_open ("{your.imap.host:143}", "username", "password");
      3
      4 echo "<p><h1>Mailboxes</h1>\n";
      5 $folders = imap_listmailbox ($mbox, "{your.imap.host:143}", "*");
      6
      7 if ($folders == false) {
      8     echo "Call failed<br>\n";
      9 } else {
     10     while (list ($key, $val) = each ($folders)) {
     11         echo $val."<br>\n";
     12     }
     13 }
     14
     15 echo "<p><h1>Headers in INBOX</h1>\n";
     16 $headers = imap_headers ($mbox);
     17
     18 if ($headers == false) {
     19     echo "Call failed<br>\n";
     20 } else {
     21     while (list ($key,$val) = each ($headers)) {
     22         echo $val."<br>\n";
     23     }
     24 }
     25
     26 imap_close($mbox);
     27

Prev | Home | Next
imap_num_recent | Up | imap_ping