PHP - imap_headerinfo() 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_headerinfo() function accepts a resource value representing an IMAP stream, an integer value representing a particular message as parameters and, reads the header of the specified message.
imap_headerinfo($imap_stream ,$msg [,fromlength, $subjectlength, $defaulthost]);

imap_stream (Mandatory)
This is a string value representing an IMAP stream, return value of the imap_open() function.
msg (Mandatory)
This is an integer value representing the message/mail number.
fromlength (Optional)
This is an integer value representing the length of the fetchfrom property.
subjectlength (Optional)
This is an integer value representing the length of the fetchsubject property.
This function returns an object representing the headers of the specified message in case of success and a Boolean value which is FALSE in case of failure.
This function was first introduced in PHP Version 4 and works in all the later versions.
Following example demonstrates the usage of the imap_headerinfo() 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 headers of all messages		 
         print("Headers of all messages: "."<br>");
         $res = imap_headerinfo($imap, 2);	
         print_r($res);		     
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>
This will generate the following output −
Connection established....
Headers of all messages:
stdClass Object ( [date] => Thu, 22 Oct 2020 20:10:52 +0530 [Date] => Thu, 
22 Oct 2020 20:10:52 +0530 [message_id] => [toaddress] => 
tutorialspoint.test@gmail.com [to] => Array ( [0] => stdClass Object ( 
[mailbox] => tutorialspoint.test [host] => gmail.com ) ) [fromaddress] =>
Sender [from] => Array ( [0] => stdClass Object ( [personal] => Sender 
[mailbox] => sample.test[host] => gmail.com ) ) [reply_toaddress] => 
Sender [reply_to] => Array ( [0] => stdClass Object ( [personal] => 
Sender [mailbox] => sample.test[host] => gmail.com ) ) [senderaddress] =>
Sender [sender] => Array ( [0] => stdClass Object ( [personal] => Sender 
[mailbox] => sample.test[host] => gmail.com ) ) [Recent] => [Unseen] =
> U [Flagged] => [Answered] => [Deleted] => [Draft] => [Msgno] =
> 2 [MailDate] => 22-Oct-2020 14:41:31 +0000 [Size] => 4858 [udate] =>
1603377691 )

Following is another example of the above 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 headers of all messages		 
         print("Headers of all messages: "."<br>");
            
         for($i=1; $i<=imap_num_msg($imap); $i++) {
         $res = imap_headerinfo($imap, $i);
            print($res->toaddress);
            print("<br>");
            print($res->fromaddress);
            print("<br>");
            print($res->date);
            print("<br>");
            print($res->Size);	
            print("<br>");	
            print("<br>");		  
         }
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>
This will generate the following output −
Connection established....
Headers of all messages:
tutorialspoint.test@gmail.com
Sender
Thu, 22 Oct 2020 20:10:17 +0530
4857

tutorialspoint.test@gmail.com
Sender
Thu, 22 Oct 2020 20:10:52 +0530
4858

tutorialspoint.test@gmail.com
Sender
Sun, 25 Oct 2020 16:11:22 +0530
4880

tutorialspoint.test@gmail.com
Sender
Sun, 25 Oct 2020 17:22:41 +0530
4882

tutorialspoint.test@gmail.com
Sender
Sun, 25 Oct 2020 17:23:10 +0530
4884

tutorialspoint.test@gmail.com
Sender
Sun, 25 Oct 2020 17:24:25 +0530
4883

tutorialspoint.test@gmail.com
Sender
Mon, 26 Oct 2020 12:31:14 +0530
4888


    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