PHP - imap_sort() 函式



PHP−IMAP 函式幫助您訪問電子郵件帳戶,IMAP 代表 **I**nternet **M**ail **A**ccess **P**rotocol,使用這些函式,您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

**imap_sort()** 函式接受表示 IMAP 流的資源值、表示搜尋條件的字串值和整數值(用於排序)作為引數,並按指定的排序順序檢索給定郵箱中的郵件。

語法

imap_sort($imap_stream, $criteria, [$options, $charset]);

引數

序號 引數和描述
1

imap_stream(必填)

這是一個字串值,表示 IMAP 流,是 **imap_open()** 函式的返回值。

2

criteria(必填)

這是一個字串值,表示搜尋條件。

3

reverse(必填)

這是一個整數值,表示排序順序。1 表示反向排序。

4

options(可選)

這是一個字串值,表示可選值 SE_UID。設定後,返回的陣列包含 UID 而不是郵件序列。

5

search_criteria(可選)

這是一個字串值,表示搜尋條件。

6

$charset(可選)

這是一個字串值,表示在搜尋期間使用的 MIME 字元集。

返回值

此函式返回一個數組,其中包含表示給定郵箱中郵件的郵件編號/UID,並按排序順序排列。

PHP 版本

此函式首次引入 PHP 版本 4,並在所有後續版本中均有效。

示例

以下示例演示了 **imap_sort()** 函式的使用方法:

<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>

輸出

這將生成以下輸出:

Connection established....
Results of sorting:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7)

示例

以下是此函式的另一個示例:

<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>

輸出

這將生成以下輸出:

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 )

示例

以下是上述函式使用可選引數的示例:

<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>

輸出

這將生成以下輸出:

Connection established....
Contents of the first message:
52

51

50

49

42

20

19
php_function_reference.htm
廣告

© . All rights reserved.