Search code examples
procmail

Procmail: Move to folder and mark as read


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?


Solution

  • 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.