PHP - Fonction imap_rename ()

Les fonctions PHP-IMAP vous aident à accéder aux comptes de messagerie, IMAP signifie IInternet Mail Aaccès Protocol en utilisant ces fonctions, vous pouvez également travailler avec les protocoles NNTP, POP3 et les méthodes d'accès aux boîtes aux lettres locales.

le imap_rename()function est un alias de imap_renamemailbox (). Il accepte une valeur de ressource représentant un flux IMAP, deux valeurs de chaîne représentant l'ancien nom et le nouveau nom de la boîte aux lettres comme paramètres et renomme la boîte aux lettres donnée.

Syntaxe

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

Paramètres

Sr. Non Paramètre et description
1

imap_stream (Mandatory)

Il s'agit d'une valeur de chaîne représentant un flux IMAP, valeur de retour du imap_open() fonction.

2

old_mail (Mandatory)

Il s'agit d'une valeur de chaîne représentant l'ancien nom de la boîte aux lettres.

3

new_mail(Mandatory)

Il s'agit d'une valeur de chaîne représentant le nouveau nom de la boîte aux lettres.

Valeurs de retour

Cette fonction renvoie une valeur booléenne qui est TRUE en cas de succès et FALSE en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 4 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation du imap_rename() fonction -

<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_mail2";
         $new_name = "{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX.changed_mail2";

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

Production

Cela générera la sortie suivante -

Connection established....
Name changed

Exemple

Voici un autre exemple de cette fonction -

<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_create($mailbox, imap_utf7_encode($newmailbox1));
         $res = imap_create($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_rename($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>

Production

L'exemple ci-dessus génère la sortie suivante -

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