13

a simple question: I want to move emails with a certain subject to a folder and mark them as read afterwards. Moving works for me with

:0: H
* ^Subject:.*(ThisIsMySubject)
$HOME/mail/ThisIsMyFolder

But how to mark the mails as read?

Martin
  • 337
  • 1
  • 3
  • 13
  • Depends on your MUA. Some mark as read by adding a header, some encode it in the file name (if you are using maildir, try adding a "," to the end of the file name ... I think it was comma), others have a secret index somewhere which is hard to access from outside the MUA. – tripleee Aug 06 '11 at 22:41

1 Answers1

17

Note: Updated dec. 16th 2011

Procmail solution

The following recipe works for me. .Junk is the spam folder:

MAILDIR=$HOME/Maildir
:0
* ^X-Spam-Flag: YES
{
    # First deliver to maildir so LASTFOLDER gets set
    :0 c
    .Junk

    # Manipulate the filename
    :0 ai
    * LASTFOLDER ?? ()\/[^/]+^^
    |mv "$LASTFOLDER" "$MAILDIR/.Junk/cur/$MATCH:2,S"
}

Maildrop solution

Preface: Recently I had (no, I wanted) to do the same thing with a maildropfilter. After reading man maildropfilter I concocted the following recipe. I'm sure people will find this handy - I know I do.

The example below marks new emails as read but also unread old messages.

SPAMDIRFULL="$DEFAULT/.Junk"

if ( /^X-Spam-Flag: YES$/ || \
     /^X-Spam-Level: \*\*\*/ || \
     /^Subject: \*+SPAM\*/ )
{
  exception {
    cc "$SPAMDIRFULL"
    `for x in ${SPAMDIRFULL}/new/*; do [ -f $x ] && mv $x ${SPAMDIRFULL}/cur/${x##*/}:2,S; done`
    `for x in ${SPAMDIRFULL}/cur/*:2,; do [ -f $x ] && mv $x ${SPAMDIRFULL}/cur/${x##*/}S; done`
    to "/dev/null"
  }
}

Note that the exception command might read counterintuitive. The manual states the following:

The exception statement traps errors that would normally cause maildrop to terminate. If a fatal error is encountered anywhere within the block of statements enclosed by the exception clause, execution will resume immediately following the exception clause.

drumfire
  • 943
  • 7
  • 19
  • 1
    I was searching for a maildrop filter when I found your question. But for maildrop I found an elaborate explanation [here](http://blog.sigil.org/2009/11/mark-as-read-with-maildrop.html) – drumfire Oct 06 '11 at 15:23
  • 2
    Regarding the procmail solution, some explanations would be helpful. For example, the regex is particularly cryptic, as is the renaming operation which effectively marks the message as read. By the way, the `$MAILDIR/` path prefix is superfluous. – Maëlan Nov 18 '18 at 02:33
  • @Maëlan It doesn't make sense to go over this as if it's a manual for a new procmail user. We may assume some prior knowledge here. – drumfire Nov 22 '18 at 16:31
  • 3
    Even so, assuming the reader understands that this Braille regex extracts the filename, the filename manipulation is probably not to be found in procmail’s manual as this relates to the mailbox directory structure. Why moving to `cur/` and appending `:2,S` to the filename effectively marks the message as read? I feel like you are saying me “comments are useless, just learn the semantics of your programming language.” – Maëlan Nov 22 '18 at 17:07