Mutt - Auto-delete unwanted emails

By Vitaly Parnas

This is not a spam filter. Rather, it’s an automation to quickly eliminate those frequently received and mostly unwanted emails, yet those we don’t wish to formally mark as spam.

  1. Make sure you’ve sourced and defined an alias file in your muttrc:

    source $XDG_CONFIG_HOME/mutt/aliases.muttrc
    set alias_file = $XDG_CONFIG_HOME/mutt/aliases.muttrc
    
  2. In the aliases file, define a ‘spam’ group as follows, with initially at least one address:

    alias -group spam spam test@spam.com
    
  3. Define a macro to delete all messages with from addresses in the spam group:

    macro index ds '<delete-pattern> %f spam<enter>'
    
  4. Create a script to automatically add the sender of the current email to the spam group. Place the script within your PATH. I named mine ‘mutt-add-spam’, also available in my scripts repository.

    [ -z "$MUTT_ALIAS_FILE" ] && MUTT_ALIAS_FILE="$XDG_CONFIG_HOME/mutt/aliases.muttrc"
    
    (
    from=$(sed -En '1,/^$/s/^From.*<([^>]+)>/\1/p') && 
        sed -iE '/alias -group spam/{/'$from'/q;s/$/,'$from'/}' "$MUTT_ALIAS_FILE" &&
        echo "$from added to spam group"
    )
    

    The script

    1. Reads a piped-in email and extracts the from address.
    2. If not already present, adds the address to the ‘spam’ alias group.
  5. Lastly, in muttrc define one more macro to pipe the selected message into the above script.

    macro index,pager <Esc>S "<pipe-message>mutt-add-spam; read s<enter>"
    

    Upon typing Alt+S you’ll see a confirmation message of a successful operation.

Questions, comments? Connect.