PHP - imap_num_msg() 函式



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

imap_num_msg() 函式接受表示 IMAP 流的資源值作為引數,並檢索給定郵箱中的郵件數量。

語法

imap_num_msg($imap_stream);

引數

序號 引數和描述
1

imap_stream (必填)

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

返回值

此函式在成功的情況下返回一個整數,表示郵箱中的郵件數量;失敗的情況下返回布林值 FALSE。

PHP 版本

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

示例

以下示例演示了 imap_num_msg() 函式的使用:

<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 number of messages 
         print("Number of messages: ");
         $num = imap_num_msg($imap);
         print($num);
         print("<br>");
		 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Number of messages: 7

示例

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

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         $stream = imap_open($url, $id, $pwd);
         print("Connection established....");
         print("<br>");
		 
         $num = imap_num_msg($stream);
         print("Number of messages: ".$num."\n");

         imap_append(
            $stream, $url
            , "From: sender@test.com\r\n"
            . "To: reciever@test.com\r\n"
            . "Subject: test\r\n"
            . "\r\n"
            . "this is a test message, please ignore\r\n"
         );
         print("<br>");
         print("Number of messages after append: ".imap_num_msg($stream)."\n");
         
         imap_close($stream);
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Number of messages: 10
Number of messages after append: 11
php_function_reference.htm
廣告