• PHP Video Tutorials

PHP - imap_open() Function



PHP−IMAP functions helps you to access an email accounts, IMAP stands for Internet Mail Access Protocol using these functions you can also work with NNTP, POP3 protocols and local mailbox access methods.

The imap_open() function accepts three string values representing the mailbox name/URL, user name and, password as parameters and opens stream to the specified mailbox.

Syntax

imap_open ($mailbox, $username, $password [$options, $n_retries, $params);

Parameters

Sr.No Parameter & Description
1

mailbox(Mandatory)

This is a string value representing the name/URL of the mailbox. It contains the server name, mailbox path.

2

username(Mandatory)

This is a string value representing the user name.

3

password(Mandatory)

This is a string value representing the password.

4

options (Optional)

This is an integer value representing the optional parameter which can be one or more of the following −

  • OP_READONLY

  • OP_ANONYMOUS

  • OP_HALFOPEN

  • CL_EXPUNGE

  • OP_DEBUG

  • OP_SHORTCACHE

  • OP_SILENT

  • OP_PROTOTYPE

  • OP_SECURE

5

retries (Optional)

This is an integer value representing the maximum number of attempts.

6

params (Optional)

This is an array value representing the connection parameters.

Return Values

This function returns an IMAP stream object in case of success and a Boolean value FALSE in case of failure.

PHP Version

This function was first introduced in PHP Version 4 and works in all the later versions.

Example

Following is an php program tries to establish a connection with a particular Gmail account imap_open()

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         if($mailbox){
            print("Connection established....");
         } else {
            print("Connection failed");
         }
      ?>
   </body>
</html>

Output

The above program generates the following output −

Connection established....

Example

Following is another example of this function in this we have established connection to a particular mail box and retrieved the contents of the message in it.

<html>
   <body>
      <?php
         //Establishing connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $imap = imap_open($url, $id, $pwd);
         print("Connection established...."."<br>");
         //Searching emails
         $emailData = imap_search($imap, '');
        
         if (! empty($emailData)) {  
            foreach ($emailData as $msg) {
               $msg = imap_fetchbody($imap, $msg, "1");
               print(quoted_printable_decode($msg)."<br>");                
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

The above program generates the following output −

Connection established....
This is a test mail #1.
--0000000000001bf26805af905277 Content-Type: text/plain; charset="UTF-8" test 
mail #2 --0000000000001bf26805af905277 Content-Type: text/html; charset="UTF-8" 
Content-Transfer-Encoding: quoted-printable

test mail #2
--0000000000001bf26805af905277--
test mail #3
test mail #4

Example

Following is the example of this function with optional parameters.

<html>
   <body>
      <?php
         //Establishing the connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         
         //Optional parameters
         $options = OP_READONLY;
         $retries = 10;
		 
         $mailbox = imap_open($url, $id, $pwd, $options, $retries);
         if($mailbox){
            print("Connection established....");
         } else {
            print("Connection failed");
         }
      ?>
   </body>
</html>

The above program generates the following output −

Connection established....
php_function_reference.htm
Advertisements