Search code examples
phpemailimap

PHP - imap_delete deletes the email just after visiting the webmail


I am getting my inbox emails using

$hostname = '{imap.one.com:993/imap/ssl}INBOX';

Everything works fine however, when I try to delete an email from my script, it deletes the email chosen just after I login into the server webmail in this case one.com webmail.

I am using

$msgid = '1'; //For example

imap_delete($mbox, "$msgid:$msgid");

Any idea how to get my script to delete the email without visiting the webmail server?


Solution

  • As stated in the documentation:

    Messages marked for deletion will stay in the mailbox until either imap_expunge() is called or imap_close() is called with the optional parameter CL_EXPUNGE.

    You should either:

    • Call imap_expunge() after your imap_delete call
    • Add imap_close($connection, CL_EXPUNGE); at the end of your script