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


PHP imap_search()用法及代碼示例


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

這個imap_search()接受表示 IMAP 流的資源值和表示搜索條件的字符串值作為參數,搜索郵箱並以數組的形式返回匹配的消息。

用法

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

參數

Sr.No 參數及說明
1

imap_stream (Mandatory)s

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

2

criteria (Mandatory)

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

3

options (Optional)

這是一個字符串值,表示可選值 SE_UID。在設置時,重新調整的數組包含 UID 而不是消息序列。

4

$charset (Optional)

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

返回值

此函數返回一個數組,其中包含表示成功情況下匹配消息的消息編號/UID 和失敗情況下的布爾值 FALSE。

PHP版本

這個函數最初是在 PHP 版本 4 中引入的,並且適用於所有後續版本。

示例

以下是此函數的另一個示例 -

<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>");
         print("Results of the search:"."<br>");
         
         $emailData = imap_search($imap, '');
         print_r($emailData);
	    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

這會生成以下輸出 -

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

示例

以下是此函數的另一個示例;這會讀取當前收件箱中看不見的消息 -

<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>");
         print("Contents of the matched messages:"."<br>");
         $emailData = imap_search($imap, "UNSEEN");
         foreach ($emailData as $msg) {
            $msg = imap_fetchbody($imap, $msg, "1");
            print(quoted_printable_decode($msg)."<br>");                
         }    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出 -

Connection established....
Contents of the matched messages:
Array ( [0] => 4 [1] => 5 [2] => 6 )
#sample_mail4
#sample_mail5
#sample_mail6

示例

以下是帶有可選參數的此函數的示例 -

<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>");
         print("Contents  of the matched messages:"."<br>");
         $data = imap_search($imap, "ALL", SE_UID);
         print_r($data);
    
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出 -

Connection established....
Contents of the matched messages:
Array ( 
   [0] => 19 
   [1] => 20 
   [2] => 42 
   [3] => 49 
   [4] => 50 
   [5] => 51 
)

相關用法


注:本文由純淨天空篩選整理自 PHP - imap_search() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。