PHP - imap_delete() 函式



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

imap_delete() 函式接受表示 IMAP 流的資源值、表示郵箱中郵件的整數作為引數,並將指定的郵件標記為刪除。

使用此函式標記所需的郵件後,每當呼叫 imap_expunge() 函式或使用 CL_EXPLUNGE 作為引數呼叫 close() 函式時,它們將從郵箱中刪除。

語法

imap_delete($imap_stream, $msg [, $options]);

引數

序號 引數和描述
1

imap_stream (必填)

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

2

msg (必填)

這是一個整數,表示要標記為刪除的郵件/郵件編號。

3

options(可選)

這是一個整數值,表示一個可選值 FT_UID,如果指定,則作為 msg 引數傳遞的值將被視為 UID。

返回值

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

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

示例

以下是帶可選引數的上述函式的示例 −

<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, imap_uid($imap, 5), FT_UID );
         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
php_function_reference.htm
廣告