• PHP Video Tutorials

PHP - imap_fetchstructure() 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_fetchstructure() function accepts a resource value representing an IMAP stream, an integer value representing a message in the mailbox as parameters and, reads the structure of the specified message.

Syntax

imap_fetchstructure($imap_stream, $msg [, $options]); 

Parameters

options(Optional)

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

  • FT_UID

  • FT_PEEK

  • FT_INTERNAL

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

msg (Mandatory)

This is an integer value representing the message/mail number, that is to marked for deletion.

3

msg (Mandatory)

This is an integer value representing the message/mail number, that is to marked for deletion.

Return Values

This function returns a string value representing the retrieved section of a mail/message.

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_fetchstructure() function −

<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>");
		 
         //Fetching the Structure
         print("Structure: "."<br>");
         $body = imap_fetchstructure($imap, 1);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
Structure:
stdClass Object ( 
   [type] => 1 [encoding] => 0 [ifsubtype] => 1 
   [subtype] => ALTERNATIVE [ifdescription] => 0 
   [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 
   [ifparameters] => 1 [parameters] => 
   Array ( 
      [0] => stdClass Object ( 
         [attribute] => BOUNDARY 
         [value] => 000000000000a0d34e05b24373f4 
      ) 
   ) 
   [parts] => Array (
      [0] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => PLAIN [ifdescription] => 0 
         [ifid] => 0 [lines] => 1 [bytes] => 15 
         [ifdisposition] => 0 [ifdparameters] => 0 
         [ifparameters] => 1 [parameters] => Array ( 
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
      [1] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => HTML [ifdescription] => 0 [ifid] => 0 
         [lines] => 1 [bytes] => 40 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 
         [parameters] => Array (
            [0] => stdClass Object (
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      )
   ) 
)

Example

Following is another example of this function −

<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_fetchstructure($imap, $msg);
               print($msg."<br>");      			   
            }    
         } 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
stdClass Object ( 
   [type] => 1 [encoding] => 0 [ifsubtype] => 1 
   [subtype] => ALTERNATIVE [ifdescription] => 0 
   [ifid] => 0 [ifdisposition] => 0 [ifdparameters] => 0 
   [ifparameters] => 1 [parameters] => 
   Array ( 
      [0] => stdClass Object ( 
         [attribute] => BOUNDARY [value] => 000000000000a0d34e05b24373f4 
      ) 
   ) 
   [parts] => 
   Array ( 
      [0] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 
         [lines] => 1 [bytes] => 15 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 
         [parameters] => Array ( 
            [0] => stdClass Object (
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
      [1] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => HTML [ifdescription] => 0 [ifid] => 0 
         [lines] => 1 [bytes] => 40 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 
         Array (
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
   ) 
)
stdClass Object ( 
   [type] => 1 [encoding] => 0 [ifsubtype] => 1 
   [subtype] => ALTERNATIVE [ifdescription] => 0 [ifid] => 0 
   [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 
   [parameters] => 
   Array ( 
      [0] => stdClass Object ( 
         [attribute] => BOUNDARY [value] => 000000000000bb1b8205b24375b9 
      ) 
   ) 
   [parts] => 
   Array ( 
      [0] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 
         [lines] => 1 [bytes] => 16 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 
         Array ( 
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
      [1] => stdClass Object (
         [type] => 0 [encoding] => 0 
         [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 
         [ifid] => 0 [lines] => 1 [bytes] => 42 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 
         Array ( 
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
   ) 
)

Example

Following is an example of the above function with optional parameters −

<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>");
		 
         //Fetching the Structure
         print("Structure: "."<br>");
         $body = imap_fetchstructure($imap, imap_uid($imap, 1), FT_UID);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
Structure:
stdClass Object ( 
   [type] => 1 [encoding] => 0 [ifsubtype] => 1 
   [subtype] => ALTERNATIVE [ifdescription] => 0 [ifid] => 0 
   [ifdisposition] => 0 [ifdparameters] => 0 [ifparameters] => 1 
   [parameters] => 
   Array ( 
      [0] => stdClass Object ( 
         [attribute] => BOUNDARY [value] => 000000000000a0d34e05b24373f4 
      ) 
   ) 
   [parts] => 
   Array ( 
      [0] => stdClass Object ( 
         [type] => 0 [encoding] => 0 [ifsubtype] => 1 
         [subtype] => PLAIN [ifdescription] => 0 [ifid] => 0 
         [lines] => 1 [bytes] => 15 [ifdisposition] => 0 
         [ifdparameters] => 0 [ifparameters] => 1 [parameters] => 
         Array ( 
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) [1] => stdClass Object ( 
         [type] => 0 [encoding] => 0 
         [ifsubtype] => 1 [subtype] => HTML [ifdescription] => 0 
         [ifid] => 0 [lines] => 1 [bytes] => 40 
         [ifdisposition] => 0 [ifdparameters] => 0 
         [ifparameters] => 1 [parameters] => 
         Array ( 
            [0] => stdClass Object ( 
               [attribute] => CHARSET [value] => UTF-8 
            ) 
         ) 
      ) 
   ) 
)
php_function_reference.htm
Advertisements