PHP - imap_expunge() 函式



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

您可以使用 imap_delete()、imap_mail_move() 或 imap_setflag_full() 函式中的任何一個來標記特定郵箱中的郵件/訊息以供刪除。

**imap_expunge()** 函式接受表示 IMAP 流的資源值作為引數,並刪除所有標記為要刪除的訊息。

語法

imap_expunge($imap_stream);

引數

序號 引數 & 描述
1

imap_stream (必填)

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

返回值

此函式返回布林值 TRUE。

PHP 版本

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

示例

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

<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>");
		 		 
         //Marking message for deletion
         $res = imap_delete($imap, 5);
         if($res){
            print("Message marked for deletion"."<br>");
         }		 
         //Deleting messages
         $res = imap_expunge($imap);	 	
         if($res){
            print("Message deleted");
         }		 		 
         //Closing the connection
         imap_close($imap);   
      ?>
   </body>
</html>

輸出

這將生成以下輸出:

Connection established....
Message marked for deletion
Message deleted

示例

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

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