• PHP Video Tutorials

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

Syntax

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

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

msg (Mandatory)

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

3

options(Optional)

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

  • FT_UID

  • FT_PREFETCHTEXT

  • FT_INTERNAL

Return Values

This function returns a string value representing the header of the specified mail/message.

PHP Version

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

Example

Following is an example of the imap_fetchheader() 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 contents of a message
         print("Contents of the first message: "."<br>");
         $body = imap_fetchheader($imap, 3);
         print_r($body);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established ....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 2002: a05: 6214: 1625: 
0: 0: 0: 0 with SMTP id e5csp4406831qvw; Mon, 26 Oct 2020 00:01:56 −0700 
(PDT) X-Received: by 2002: a05: 6830: 1308 :: with SMTP id 
p8mr1957837otq.330.1603695716288; Mon, 26 Oct 2020 00:01:56 −0700 (PDT) ARC
−Seal: i = 1; a = rsa−sha256; t = 1603695716; cv = none; d = 
google.com; s = arc-20160816; b = 
Xm8jmMAo9BHvDPfEAHphx6Siu3z1V6Kp9EBFAlZ8pq5efBjIEA / zBXvKTzf4XVct + v +  . . . . 
. . . . . . . . . . . 

Example

This 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>");
		 
         //Fetching the contents of a message
         print("Contents of the first message: "."<br>");
		 
         $emailData = imap_search($imap, '');
         foreach ($emailData as $msg) {
            $header = imap_fetchheader($imap, $msg);
            print($header);
            print("<br>");              
         }    	     
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

Output

This will generate the following output −

Connection established....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 
2002:a05:6214:1625:0:0:0:0 with SMTP id e5csp1568707qvw; Thu, 22 Oct 2020 
07:40:56 -0700 (PDT) X-Received: by 2002:aca:750b:: with SMTP id 
q11mr1798627oic.163.1603377656521; Thu, 22 Oct 2020 07:40:56 -0700 (PDT) 
ARC-Seal: i=1; a=rsa-sha256; t=1603377656; cv=none; d=google.com; s=arc-20160816; 
b=zSBoZxFjMeKTU6s4IfuwImWLA+EHPl+rbEcemnErz+th7yY5+qDdO3bqR7R14ZOQwf

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 contents of a message
         print("Contents of the first message: "."<br>");
         $body = imap_fetchheader($imap, imap_uid($imap, 7), FT_UID);
         print_r($body);
         print_r($body);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

This will generate the following output −

Connection established ....
Contents of the first message:
Delivered-To: tutorialspoint.test@gmail.com Received: by 2002: a05: 6214: 1625: 
0: 0: 0: 0 with SMTP id e5csp4406831qvw; Mon, 26 Oct 2020 00:01:56 −0700 
(PDT) X−Received: by 2002: a05: 6830: 1308 :: with SMTP id 
p8mr1957837otq.330.1603695716288; Mon, 26 Oct 2020 00:01:56 −0700 (PDT) 
ARC-Seal: i = 1; a = rsa-sha256; t = 1603695716; cv = none; d = google.com; s = 
arc-20160816; b = Xm8jmMAo9BHvDPfEAHphx6Siu3z1V6Kp9EBFAlZ8pq5efBjIEA / 
zBXvKTzf4XVct + v +  . . . . . . . . . . . . . . . 
php_function_reference.htm
Advertisements