PHP - imap_close() 函式



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

**imap_close()** 函式接受表示 IMAP 流的資源值作為引數,並關閉當前流。

語法

imap_close($imap_stream[, $flag]);

引數

序號 引數及描述
1

imap_stream (必填)

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

2

flag(必填)

這是一個字串值,表示可選的標誌值,如果設定為 **CL_EXPUNGE**,則該函式會在關閉郵箱(流)之前刪除所有標記為已刪除的訊息。

返回值

此函式返回一個布林值,成功時為 TRUE,失敗時為 FALSE。

PHP 版本

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

示例

以下示例演示了 **imap_close()** 函式的用法:

<html>
   <body>
      <?php
         //Establishing the connection
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "tutorialspoint.test@gmail.com";
         $pwd = "cohondob_123";
         
         //Optional parameters
         $options = OP_READONLY;
         $retries = 10;
         $mailbox = imap_open($url, $id, $pwd, $options, $retries);
         
         if($mailbox){
            print("Comnnection established....");
         } else {
            print("Connection failed");
         }
		 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....

示例

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

<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 inbox: "."<br>");
         $emailData = imap_search($imap, '');
         foreach ($emailData as $msg) {
            $msg = imap_fetchbody($imap, $msg, "1");
            print(quoted_printable_decode($msg)."<br>");                
         }    
         //Marking message for deletion
         imap_delete($imap, 5);
         
         //Deleting messages
         imap_expunge($imap);	 		 
		 
         print("Contents of inbox after deletion: "."<br>");
         $emailData = imap_search($imap, '');
         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 inbox:
#sample_mail1
#sample_mail2
#sample_mail3
#sample_mail4
#sample_mail5
Contents of inbox after deletion:
#sample_mail1
#sample_mail2
#sample_mail3
#sample_mail4
php_function_reference.htm
廣告