imap_createmailbox
(PHP 4, PHP 5, PHP 7)
imap_createmailbox — Create a new mailbox
Описание
bool imap_createmailbox
( resource
$imap_stream
, string $mailbox
)
Creates a new mailbox specified by mailbox
.
Список параметров
-
imap_stream
-
Поток IMAP, полученный из imap_open().
-
mailbox
-
The mailbox name, see imap_open() for more information. Names containing international characters should be encoded by imap_utf7_encode()
Возвращаемые значения
Возвращает TRUE
в случае успешного завершения или FALSE
в случае возникновения ошибки.
Примеры
Пример #1 imap_createmailbox() example
<?php
$mbox = imap_open("{imap.example.org}", "username", "password", OP_HALFOPEN)
or die("can't connect: " . imap_last_error());
$name1 = "phpnewbox";
$name2 = imap_utf7_encode("phpnewböx"); // phpnewb&w7Y-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 finally remove it to restore
// your inbox to its initial state
if (@imap_createmailbox($mbox, imap_utf7_encode("{imap.example.org}INBOX.$newname"))) {
$status = @imap_status($mbox, "{imap.example.org}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, "{imap.example.org}INBOX.$newname", "{imap.example.org}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, "{imap.example.org}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);
?>
Смотрите также
- imap_renamemailbox() - Rename an old mailbox to new mailbox
- imap_deletemailbox() - Delete a mailbox
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с почтой
- IMAP, POP3 и NNTP
- imap_8bit
- imap_alerts
- imap_append
- imap_base64
- imap_binary
- imap_body
- imap_bodystruct
- imap_check
- imap_clearflag_full
- imap_close
- imap_create
- imap_createmailbox
- imap_delete
- imap_deletemailbox
- imap_errors
- imap_expunge
- imap_fetch_overview
- imap_fetchbody
- imap_fetchheader
- imap_fetchmime
- imap_fetchstructure
- imap_fetchtext
- imap_gc
- imap_get_quota
- imap_get_quotaroot
- imap_getacl
- imap_getmailboxes
- imap_getsubscribed
- imap_header
- imap_headerinfo
- imap_headers
- imap_last_error
- imap_list
- imap_listmailbox
- imap_listscan
- imap_listsubscribed
- imap_lsub
- imap_mail_compose
- imap_mail_copy
- imap_mail_move
- imap_mail
- imap_mailboxmsginfo
- imap_mime_header_decode
- imap_msgno
- imap_num_msg
- imap_num_recent
- imap_open
- imap_ping
- imap_qprint
- imap_rename
- imap_renamemailbox
- imap_reopen
- imap_rfc822_parse_adrlist
- imap_rfc822_parse_headers
- imap_rfc822_write_address
- imap_savebody
- imap_scan
- imap_scanmailbox
- imap_search
- imap_set_quota
- imap_setacl
- imap_setflag_full
- imap_sort
- imap_status
- imap_subscribe
- imap_thread
- imap_timeout
- imap_uid
- imap_undelete
- imap_unsubscribe
- imap_utf7_decode
- imap_utf7_encode
- imap_utf8
Коментарии
One should understand that even though it says "create mailbox", you are really creating a FOLDER. Now, as a imap admin you can create mailboxes and more with this function.
So in reality, you are always creating folders when creating "mailboxes". Mail admin's get this, but programmers may not understand the concept completely.
If you auth a single user account and use these functions, they will not create mailboxes where mail is delivered, they will actually create a folder where you can copy messages to.
Here is a translation:
imap_createmailbox = create a folder in the account for the current authenticated user's imap session (imap_open)
imap_deletemailbox = delete a folder (and the email in it) for the current authenticated user's imap session (imap_open)
imap_getmailboxes = get all your folders for the current authenticated user's imap session (imap_open)
imap_renamemailbox = Rename a folder for the current authenticated user's imap session (imap_open)
================
Here is a quick class to login to an account, generate all of your base folders, and return the connection, success message and returns all the base folders for an imap account using PHP5:
<?php
class Imap {
public $folders;
public $connection;
public function login($user, $pass) {
$mbox = @imap_open("{imap.example.org:143}", $user, $pass);
if(!$mbox)
return ('Your login failed for user <strong>'.$user.'</strong>. Please try to enter your username and password again.<br />');
// Login worked, let us begin!!!!....
// gather folder lost...
$fldrs_made = 0;
$folders = imap_listmailbox($mbox, "{localhost:143}", "*");
// create the default folders....
if(1 === mailgui::create_default_folders($mbox,$folders)) {
$folders = imap_listmailbox($mbox, "{localhost:143}", "*");
$fldrs_made = 1;
}
sort($folders);
$this->folders = $folders;
$this->connection = $mbox;
if(1 === $fldrs_made)
return ('User logged in successfully as '.$user.'. This is your first time logging in, welcome to our webmail!!!<br />');
else
return ('User logged in successfully as '.$user.'.<br />');
}
private function create_default_folders($imap_stream, $folders) {
$change=0;
if(!in_array('{imap.example.org}TRASH',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}TRASH"));
$change=1;
}
if(!in_array('{imap.example.org}SENT',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}SENT"));
$change=1;
}
if(!in_array('{imap.example.org}SPAM',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}SPAM"));
$change=1;
}
if(!in_array('{imap.example.org}SENT',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}SENT"));
$change=1;
}
if(!in_array('{imap.example.org}SENT',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}DRAFTS"));
$change=1;
}
if(!in_array('{imap.example.org}MY_FOLDERS',$folders)) {
@imap_createmailbox($imap_stream, imap_utf7_encode("{imap.example.org:143}PERSONAL EMAIL"));
$change=1;
}
return $change;
}
public function close_mail_connection() {
@imap_close($this->connection);
}
}
// usage, create a form, post it....
if($_POST['imap_username'] && $_POST['imap_password']) {
$imap_login = new Imap();
$imap_login->login($_POST['imap_username'],$_POST['imap_password']);
// Do some mail stuff here, like get headers...., use obj connection
$message_headers = imap_mailboxmsginfo($imap_login->connection);
// show the folders....
print_r($imap_login->folders, true);
print '<br /><hr size="1" noshade />';
print_r($message_headers, true);
// close the connection
$imap_login->close_mail_connection();
}
?>