imap_createmailbox

(PHP 3, PHP 4, PHP 5)

imap_createmailbox -- Create a new mailbox

Description

bool imap_createmailbox ( resource imap_stream, string mbox )

imap_createmailbox() creates a new mailbox specified by mbox. Names containing international characters should be encoded by imap_utf7_encode()

如果成功则返回 TRUE,失败则返回 FALSE。.

例子 1. imap_createmailbox() example

<?php
$mbox
= imap_open("{your.imap.host}", "username", "password", OP_HALFOPEN)
     or die(
"can't connect: " . imap_last_error());

$name1 = "phpnewbox";
$name2 = imap_utf7_encode("phpnewb&ouml;x");

$newname = $name1;

echo
"Newname will be '$name1'<br />\n";

// we will now create a new mailbox "phptestbox" in your inbox folder,
// check its status after creation and finaly remove it to restore
// your inbox to its initial state

if (@imap_createmailbox($mbox, imap_utf7_encode("{your.imap.host}INBOX.$newname"))) {
    
$status = @imap_status($mbox, "{your.imap.host}INBOX.$newname", SA_ALL);
    if (
$status) {
        echo
"your new mailbox '$name1' has the following status:<br />\n";
        echo
"Messages:   " . $status->messages    . "<br />\n";
        echo
"Recent:     " . $status->recent      . "<br />\n";
        echo
"Unseen:     " . $status->unseen      . "<br />\n";
        echo
"UIDnext:    " . $status->uidnext     . "<br />\n";
        echo
"UIDvalidity:" . $status->uidvalidity . "<br />\n";

        if (
imap_renamemailbox($mbox, "{your.imap.host}INBOX.$newname", "{your.imap.host}INBOX.$name2")) {
            echo
"renamed new mailbox from '$name1' to '$name2'<br />\n";
            
$newname = $name2;
        } else {
            echo
"imap_renamemailbox on new mailbox failed: " . imap_last_error() . "<br />\n";
        }
    } else {
        echo
"imap_status on new mailbox failed: " . imap_last_error() . "<br />\n";
    }
    
    if (@
imap_deletemailbox($mbox, "{your.imap.host}INBOX.$newname")) {
        echo
"new mailbox removed to restore initial state<br />\n";
    } else {
        echo
"imap_deletemailbox on new mailbox failed: " . implode("<br />\n", imap_errors()) . "<br />\n";
    }

} else {
    echo
"could not create new mailbox: " . implode("<br />\n", imap_errors()) . "<br />\n";
}

imap_close($mbox);
?>

See also imap_renamemailbox(), imap_deletemailbox() and imap_open() for the format of mbox names.


add a note add a note User Contributed Notes
14-Sep-2003 04:32
// is not a complete code but enough for add new folder/subfolder
// hope this can save your time :-)

<?php
# PULL ADDITIONAL FILES
include_once ("common.php");

$conn = @imap_open("\{$server/$serverType}", $user, $pass) ;

// add folder
if(isset($_REQUEST['AddFolder']))
{
  if(
$_REQUEST['FolderName']!="")
  {
  
$FolderName = $_REQUEST['FolderName'];
  
$PrevFolder = $_REQUEST['PrevFolder'];
   if(
$PrevFolder != "")
   {
$PrevFolder = $PrevFolder ."/";}
   if(@
imap_createmailbox($conn, imap_utf7_encode("\{$server}$PrevFolder$FolderName")))
   {
     echo
"folder created !";
   }
  
?>
    <script language="JavaScript">
   {
     parent.leftmenu.location.href = "leftmenu.php";
   }
   </SCRIPT>
<?php
 
}
}

echo
'<table border=1>';
echo
"<form name='folderform' action='".$_SERVER['PATH_INFO']."' method='get'>";
echo
"<tr>";   
echo
"<td>Add new folder : <input name='FolderName' size=20></td>";
echo
"<td>After the folder : <select name='PrevFolder'>";
$folders = imap_list($conn, "\{$server/$serverType}", "*");

if (
$folders == false)
{ echo
"Call failed<br>\n"; }
else
{
   echo
"<option value=''></option>";
   while (list (
$key, $val) = each ($folders))
   {
      
$val = preg_replace("\{$server/$serverType}" , "" , $val);
      
$val = substr($val,2);
       echo
"<option value='$val'>$val</option>";
   }
}
echo
"</select></td>";
echo
"<td><input type='submit' name='AddFolder' value='Add now'></td>"
echo
"</tr>";   
echo
"</form></table><P>";

imap_close($conn);
?>
mustu17 at yahoo dot com
26-Aug-2000 03:48
we can't get support of imap_utf7_encode()  function in imap4
when we try to create mailbox using base64_encode()
function then it doesn't create.
cmoyer at chekinc dot com
03-Aug-2000 08:31
The example simply creates a new mailbox in the users directory.
It won't work with POP.