• PHP Video Tutorials

PHP - imap_getmailboxes() Function



PHP−IMAP functions helps you to access 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_getmailboxes() function accepts a resource value representing an IMAP stream, two string values representing the server specification and mailbox hierarchy as parameters and, reads/retrieves information on the mailboxes.

Syntax

imap_getmailboxes($imap_stream, $ref, $pattern);

Parameters

Sr.No Parameter & Description
1

imap_stream (Mandatory)

This is a string value representing an IMAP stream, return value of the imap_open() function.

2

ref (Mandatory)

This is a string value representing the server specification.

3

pattern (Mandatory)

This is a string value representing the start of the mailbox hierarchy search.

Return Values

This function returns an array of objects where each object holds information about the mailboxes.

PHP Version

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

Example

Following example demonstrates the usage of the imap_getMailboxes() function.

<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);
         print("Connection established....");
         print("<br>");

         //Retrieving the contents of mail boxes
         $list = imap_getmailboxes($mailbox, $url, "*");
         print_r($list);         
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
Array ( 
   [0] => stdClass Object ( 
      [name] => {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX [attributes] => 64 [delimiter] => / 
   ) 
)

Example

Following is another example of this function.

<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);
         print("Connection established....");
         print("<br>");

         //Retrieving the contents of mail boxes
         $list = imap_getmailboxes($mailbox, $url, "*");

         if (is_array($list)) {
            foreach ($list as $key => $val) {		   
               print("Name: ".imap_utf7_decode($val->name) ."<br>");
               print("Delimiter: " . $val->delimiter . "<br>");
               print("Attributes: ".$val->attributes . "<br />");
               print("<br>");
            }         
         } else {
            print(“List empty”);
         }
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
Name: {imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
Delimiter: /
Attributes: 64
php_function_reference.htm
Advertisements