How to categorize incoming emails

emailimapmutt

I have a Gmail account, mutt is configured to get the mail through IMAP. Yesterday I subscribed to a mailing list and now my personal emails are mixed up with the ones from the list.

The list emails are addressed to me and lilypond-user@gnu.org. How can I tell mutt to move all such emails to a separate file, so they wouldn't be mixed with my emails. But I still could read them, opening that file?

Best Answer

There are several options depending on what you are wanting to achieve and what you are wanting to do to get there.

  • Get the IMAP server to do the filtering for you.

    This is sometimes an option in web-mail based solutions and allows you to filter the messages based on e.g. the addresses listed in the To: or Cc: header of each mail. I'm not familiar with Gmail's offerings in this regard.

  • Manually mark the messages in mutt and copy them to a new folder on the IMAP server, or to a local mailbox.

    Mark the messages you want to move with T followed by the search pattern ~C lilypond-user@gnu.org (this tags all messages that were either sent directly to or Cc-ed to the address lilypond-user@gnu.org). Then press ; followed by s to apply the "save" (move) command to all tagged messages. Then enter the IMAP folder path you want to save the messages to.

    The IMAP folder path should be specified as

    imap[s]:[user[:pw]@]imapserver.example.com[:port]/path
    

    Just to say that the IMAP server that I have access to doesn't like this. There are no errors, but the messages are clearly not copied. Test it on a less important message first! You may obviously save the messages locally instead!

    You may also define a macro in mutt to do this.

  • Download the messages from the IMAP server and filter and read them locally.

I tend to download the messages off the IMAP server using fetchmail. This gives me the opportunity to do my own spam filtering and mail sorting on my local machine.

For both these tasks I use procmail1 which is a fairly advanced mail processing program.

The essential configuration for fetchmail that I use is

poll myimapserver.example.com
    protocol imap
    user "myimapusername" password "myimappassword" is "mylocalusername"
    mda "/usr/local/bin/procmail -m $HOME/.procmailrc"
    ssl sslcertfile /etc/ssl/cert.pem sslcertck
    idle

This will fetch any new messages off the IMAP server as they arrive, and deliver them to procmail for processing. Paths etc. will be different on your system.

Then I filter with procmail using a configuration ($HOME/.procmailrc) like

MAILDIR="$HOME/Mail"
DEFAULT="inbox/"

:0
* ^TO_announce@openbsd.org
openbsd-announce/

:0
* ^TO_misc@openbsd.org
openbsd-misc/

... for two of the mailing lists I'm on (they will be stored in subdirectories under $HOME/Mail). Mail not matching any patterns will be stored in $HOME/Mail/inbox as specified by MAILDIR and DEFAULT. I'm using Maildir mailboxes. Remove the trailing slashes on the paths to get mbox mailboxes.


1 Note that procmail is retired. I was not aware of this as I've been using is since the 90's without much consideration for any of the up-and-coming alternatives. It seems, after some gentle browsing on the interwebs, that maildrop is considered a good alternative to procmail, and I might look into moving my filtering over to maildrop myself.

Related Question