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.
imap_open ($mailbox, $username, $password [$options, $n_retries, $params);

mailbox(Mandatory)
This is a string value representing the name/URL of the mailbox. It contains the server name, mailbox path.
username(Mandatory)
This is a string value representing the user name.
password(Mandatory)
This is a string value representing the password.
options (Optional)
This is an integer value representing the optional parameter which can be one or more of the following −
OP_READONLY
OP_READONLY
OP_ANONYMOUS
OP_ANONYMOUS
OP_HALFOPEN
OP_HALFOPEN
CL_EXPUNGE
CL_EXPUNGE
OP_DEBUG
OP_DEBUG
OP_SHORTCACHE
OP_SHORTCACHE
OP_SILENT
OP_SILENT
OP_PROTOTYPE
OP_PROTOTYPE
OP_SECURE 
OP_SECURE 
retries (Optional)
This is an integer value representing the maximum number of attempts.
params (Optional)
This is an array value representing the connection parameters.
This function returns an IMAP stream object in case of success and a Boolean value FALSE in case of failure.
This function was first introduced in PHP Version 4 and works in all the later versions.
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>
The above program generates the following output −
Connection established....

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>
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

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....


    45 Lectures 
     9 hours 

 Malhar Lathkar

    34 Lectures 
     4 hours 

 Syed Raza

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    17 Lectures 
     1 hours 

 Nivedita Jain

    100 Lectures 
     34 hours 

 Azaz Patel

    43 Lectures 
     5.5 hours 

 Vijay Kumar Parvatha Reddy
 Print
 Add Notes
 Bookmark this page