Procmail: Move to folder and mark as read
Asked Answered
N

1

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?

Nathanson answered 4/5, 2011 at 11:47 Comment(1)
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.Hayleyhayloft
G
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.

Grochow answered 6/10, 2011 at 15:20 Comment(4)
I was searching for a maildrop filter when I found your question. But for maildrop I found an elaborate explanation hereGrochow
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.Vosges
@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.Grochow
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.”Vosges

© 2022 - 2024 — McMap. All rights reserved.