
- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Environment Setup
- PHP - Syntax Overview
- PHP - Variable Types
- PHP - Constants
- PHP - Operator Types
- PHP - Decision Making
- PHP - Loop Types
- PHP - Arrays
- PHP - Strings
- PHP - Web Concepts
- PHP - GET & POST
- PHP - File Inclusion
- PHP - Files & I/O
- PHP - Functions
- PHP - Cookies
- PHP - Sessions
- PHP - Sending Emails
- PHP - File Uploading
- PHP - Coding Standard
- Advanced PHP
- PHP - Predefined Variables
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Bugs Debugging
- PHP - Date & Time
- PHP & MySQL
- PHP & AJAX
- PHP & XML
- PHP - Object Oriented
- PHP - For C Developers
- PHP - For PERL Developers
- PHP Form Examples
- PHP - Form Introduction
- PHP - Validation Example
- PHP - Complete Form
- PHP login Examples
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP AJAX Examples
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML Example
- PHP - XML Introduction
- PHP - Simple XML
- PHP - Simple XML GET
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Frame Works
- PHP - Frame Works
- PHP - Core PHP vs Frame Works
- PHP Design Patterns
- PHP - Design Patterns
- PHP Function Reference
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Useful Resources
- PHP - Discussion
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 UID’s 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/UID’s 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