Showing posts with label PHP DELETE MESSAGES. Show all posts
Showing posts with label PHP DELETE MESSAGES. Show all posts

Wednesday, 12 September 2018

Deleting messages with PHP IMAP and Gmail

My last PHP post looked at how to delete email messages using PHP's IMAP functions. This is straight forward on a regular POP3 or IMAP mail server but deleting a message when connected to Gmail just removes it from the inbox and it's still available in "All Mail" rather than being moved into the trash. This post looks at how to move an email into Gmail's trash with PHP IMAP.
Using imap_delete will simply remove the message from the inbox but it will still be available in "All Mail" and won't be in the trash:
imap_delete($connection, $msgno);
So instead the email message must be moved into the trash using the imap_mail_move function. The second parameter for this function takes a range/list and not a single message number, so to move $msgno you need to set the range as "$msgno:$msgno" like so:
imap_mail_move($connection, "$msgno:$msgno", '[Google Mail]/Trash');
The actual name of the trash folder will vary depending on the language settings. For example I have Gmail set to using "English (UK)" so the trash folder is actually called the "Bin" and to move messages into it requires this instead:
imap_mail_move($connection, "$msgno:$msgno", '[Google Mail]/Bin');
I've also seen people refer to the bracketed part as [Gmail] instead of [Google Mail] so it would pay to use the imap_list() function to work out exactly what the trash folder is called and assigning it to a variable. An example of doing this can be found in my Open a mailbox other than the INBOX with PHP IMAP post.

Related posts:

Deleting messages with PHP IMAP

I've been writing a series about sending Google Analytics data by email and then processing the emails with PHP's IMAP functions. Once the email has been processed you'll probably want to delete the emails from the mailbox afterwards so this email looks at how to delete emails using PHP's IMAP functions.
Deleting a message is really simple:
imap_delete($connection, $msgno);
where $connection is your already established mailbox connection, and $msgno is the number of the message that should be deleted. See my looping through messages post which shows how you would get the message number by either looping through the messges in the mailbox or using the imap_search function to do a search.
The above function will flag the message to be deleted when connecting to an imap mailbox so you'll either need to then call the imap_expunge function to delete it completely or add the CL_EXPUNGE parameter to the imap_close function when you disconnect.
Using imap_expunge:
imap_expunge($connection);
When calling imap_close:
imap_close($connection, CL_EXPUNGE);
Note that when connecting using POP3 instead of IMAP, the deletion flag is not saved between connections so you need to call imap_expunge during the connection (or use the CL_EXPUNGE flag when disconnecting) to actually purge the messages. With IMAP you could instead expunge them on the next connection instead.
A note on Gmail: imap_delete doesn't delete messages when connecting to a Gmail mailbox, it just removes them from the inbox. My next PHP post on Thursday will look at how to move a Gmail message from the inbox into the trash using Gmail.

Related posts: