PHP - imap_header() 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_header() function is an alias of iamp_headerinfo(), it 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.
Syntax
imap_header($imap_stream ,$msg [,fromlength, $subjectlength, $defaulthost]);
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. |
| 3 |
fromlength (Optional) This is an integer value representing the length of the fetchfrom property. |
| 4 |
subjectlength (Optional) This is an integer value representing the length of the fetchsubject property. |
Return Values
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.
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_header() 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_header ($imap, 2);
print_r($res);
//Closing the connection
imap_close($imap);
?>
</body>
</html>
Output
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 )
Example
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_header($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>
Output
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