PHP - imap_sort() 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_sort() accepts a resource value representing an IMAP stream, and a string value representing the search criteria and an integer value (for sorting) as parameters and retrieves the messages in the given mailbox in the specified sorted order.
Syntax
imap_sort($imap_stream, $criteria, [$options, $charset]);
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 |
criteria (Mandatory) This is a string value representing the search criteria. |
| 3 |
reverse (Mandatory) This is an integer value representing the sort order. 1 for reverse sorting. |
| 4 |
options (Optional) This is a string value representing the optional value SE_UID. On setting the array returned contains UIDs instead of message sequences. |
| 5 |
search_criteria (Optional) This is a string value representing the search criteria. |
| 6 |
$charset (Optional) This is a string value representing the MIME character set to use during the search. |
Return Values
This function returns an array which contains the message numbers/UIDs representing the messages in the given mailbox in sorted order.
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_sort() 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("Result of sorting: "."<br>");
$res = imap_sort($imap, SORTDATE, 0);
print_r($res);
//Closing the connection
imap_close($imap);
?>
</body>
</html>
Output
This will generate the following output −
Connection established.... Results of sorting: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7)
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>");
//Fetching the contents of a message
print("Contents of the first message: "."<br>");
print_r(imap_sort($imap, SORTDATE, 0));
print("<br>");
print_r(imap_sort($imap, SORTARRIVAL, 0));
print("<br>");
print_r(imap_sort($imap, SORTFROM, 0));
print("<br>");
print_r(imap_sort($imap, SORTSUBJECT, 0));
print("<br>");
print_r(imap_sort($imap, SORTTO, 0));
print("<br>");
print_r(imap_sort($imap, SORTCC, 0));
print("<br>");
print_r(imap_sort($imap, SORTSIZE, 0));
//Closing the connection
imap_close($imap);
?>
</body>
</html>
Output
This will generate the following output −
Connection established.... Contents of the first message: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 6 [5] => 5 [6] => 7 )
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>");
$res = imap_sort($imap, SORTDATE, 1, SE_UID, "ALL", "");
foreach ($res as $msg) {
print($msg);
print("<br>");
print("<br>");
}
//Closing the connection
imap_close($imap);
?>
</body>
</html>
Output
This will generate the following output −
Connection established.... Contents of the first message: 52 51 50 49 42 20 19