Search code examples
imap

How can I filter then modify emails using IMAP?


I have asked this question in a different post here on SO:

How can a read receipt be suppressed?

I have been doing some research of my own to try and solve this problem and accessing the email account via IMAP seems like it is going to be a good solution. I have successfully been able to access my own inbox and mark messages as read with no issue.

I have been asked to perform the same task on an inbox that contains over 23,000 emails. I would like to run the test on a small amount of emails from that inbox before letting the whole 23,000 get it.

Here is the code I have been running via telnet:

LOGIN [email protected] password
SELECT Inbox
STORE 1:* flags \Seen  'this line marks all the emails as read

So my question is, how can I execute that STORE command on a specific group of emails ... say emails that are going to / coming from a specific account? Is there a way to concatenate the commands like a FETCH then the STORE? Or is there a better way to go about getting a collection of emails based on certain criteria and then modifying ONLY those emails that can be accomplished through IMAP?


Solution

  • Take a look at the IMAP SEARCH command. The syntax is really awful, but it'll let you search for recipients or senders, for certain words in the subject or the body of messages. It will give you a list of message ids and you can use those message ids in your call to STORE.