note 130052 added to function.imap-deletemailbox
| From: | jab_creations@yahoo.com | Date: | Sat, 22 Feb 2025 00:38:31 +0000 |
| Subject: | note 130052 added to function.imap-deletemailbox | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221876@lists.php.net to get a copy of this message | ||
I want to clarify a few technicalities to spare others the aggravation I had with
imap_deletemailbox().
First off your first imap_open() should be to the folder to then imap_search() to ensure that the
user isn't inadvertently deleting messages they're not yet aware of.
Secondly if !imap_search() you want to disconnect before executing imap_deletemailbox(). If you
don't then you won't be able to avoid the following error:
PHP Request Shutdown: [CLOSED] IMAP connection broken (server response) (errflg=1)
That is because you just deleted the folder and the server doesn't know what to do with the
connection so it triggers the error. So again, you want to delete the folder while you're
connected to a different connection/folder combination instead.
A short example of how to cleanly delete a mail folder in PHP without triggering errors (presuming
your connection configuration is correct):
<?php
$user = 'user@domain.tld';
$pass = '[pass here]';
$mail_server = '{imap.example.com:993/ssl/imap}';
$mail_connection_folder = imap_open($mail_server.$folder_string, $user, $pass);
if ($mail_connection_folder)
{
$mail_box_messages = imap_search($mail_connection_folder, 'ALL', SE_UID);
if (!$mail_box_messages)
{
$result = imap_close($mail_connection_folder);
$mail_connection_folder = imap_open($mail_server, $user, $pass);
$result = imap_deletemailbox($mail_connection_folder,
imap_utf7_encode($mail_server.$folder_string));
}
else {}//messages in folder error.
}
else {}//Failed connection error.
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 146.70.147.121)
----
Manual Page -- https://php.net/manual/en/function.imap-deletemailbox.php
Edit -- https://main.php.net/note/edit/130052
Del: integrated -- https://main.php.net/note/delete/130052/integrated
Del: useless -- https://main.php.net/note/delete/130052/useless
Del: bad code -- https://main.php.net/note/delete/130052/bad+code
Del: spam -- https://main.php.net/note/delete/130052/spam
Del: non-english -- https://main.php.net/note/delete/130052/non-english
Del: in docs -- https://main.php.net/note/delete/130052/in+docs
Del: other reasons-- https://main.php.net/note/delete/130052
Reject -- https://main.php.net/note/reject/130052
Search -- https://main.php.net/manage/user-notes.php