PHP - imap_renamemailbox() 函数



PHP 的 IMAP 函数 giúp bạn truy cập vào các tài khoản email. IMAP là viết tắt của **I**nternet **M**ail **A**ccess **P**rotocol. Sử dụng các hàm này, bạn cũng có thể làm việc với các giao thức NNTP, POP3 và các phương pháp truy cập hộp thư địa phương.

函数 **imap_renamemailbox()** 接受一个表示 IMAP 流的资源值、两个表示邮箱旧名称和新名称的字符串值作为参数,并重命名给定的邮箱。

语法

imap_renamemailbox($imap_stream, $old_mail , $new_mail);

参数

序号 参数及说明
1

imap_stream (必填)

这是一个字符串值,表示 IMAP 流,是 **imap_open()** 函数的返回值。

2

old_mail (必填)

这是一个字符串值,表示邮箱的旧名称。

3

new_mail (必填)

这是一个字符串值,表示邮箱的新名称。

返回值

此函数返回一个布尔值,成功时为 TRUE,失败时为 FALSE。

PHP 版本

此函数首次引入于 PHP 4 版本,并在所有后续版本中均有效。

示例

以下示例演示了 **imap_renamemailbox()** 函数的使用:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         print("Connection established....");
         print("<br>");    
		 
         //Renaming the existing mailbox
         $old_name = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.test_mail1";
         $new_name = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail1";

         $res = imap_renamemailbox($mailbox, $old_name, $new_name);		 
         if($res){
            print("Name changed");
         } else {
            print("Error occurred");
         }
      ?>
   </body>
</html>

输出

这将生成以下输出:

Connection established....
Name changed

示例

以下是此函数的另一个示例:

<html>
   <body>
      <?php
         $url = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX";
         $id = "[email protected]";
         $pwd = "cohondob_123";
         $mailbox = imap_open($url, $id, $pwd);
         print("Connection established....");
         print("<br>");

         //Creating mailboxes
         $newmailbox1 = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.my_mail1";
         $newmailbox2 = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.my_mail2";
         $res = imap_createmailbox($mailbox, imap_utf7_encode($newmailbox1));
         $res = imap_createmailbox($mailbox, imap_utf7_encode($newmailbox2));

         //Retrieving the contents of mail boxes
         print("List of mailboxes: ");
         print("<br>");
         $list = imap_getmailboxes($mailbox, $url, "*");
         foreach ($list as $key => $val) {
            print_r($val->name);
            print("<br>");
         }	 
         //Renaming the existing mailbox
         $new_name = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail";
         imap_renamemailbox($mailbox, $newmailbox2, $new_name);
         
         //Retrieving the contents of mail boxes
         print("<br>");
         print("List of mailboxes after deletion: ");
         print("<br>");
         $list = imap_getmailboxes($mailbox, $url, "*");
         foreach ($list as $key => $val) {
            print_r($val->name);
            print("<br>");
         }	 
      ?>
   </body>
</html>

输出

这将生成以下输出:

Connection established....
List of mailboxes:
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.my_mail1
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.my_mail2

List of mailboxes after deletion:
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail
{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.my_mail1
php_function_reference.htm
广告