當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imap_sort()用法及代碼示例


PHP-IMAP 函數可幫助您訪問電子郵件帳戶,IMAP 代表I互聯網M艾爾A訪問權限Protocol 使用這些函數您還可以使用 NNTP、POP3 協議和本地郵箱訪問方法。

這個imap_sort()接受表示 IMAP 流的資源值、表示搜索條件的字符串值和整數值(用於排序)作為參數,並以指定的排序順序檢索給定郵箱中的郵件。

用法

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

參數

Sr.No 參數及說明
1

imap_stream (Mandatory)

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

2

criteria (Mandatory)

這是一個表示搜索條件的字符串值。

3

reverse (Mandatory)

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

4

options (Optional)

這是一個字符串值,表示可選值 SE_UID。設置返回的數組包含 UID 而不是消息序列。

5

search_criteria (Optional)

這是一個表示搜索條件的字符串值。

6

$charset (Optional)

這是一個字符串值,表示要在搜索期間使用的 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 - imap_sort() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。