Navigation


RSS / Atom



procmail tip

2011-01-11 , ,

I get a lot of email and sort it using procmail. Recently I’ve had to aggregate several separate mailgroups into one folder, without losing track of the original mailgroup. Procmail to the rescue.

I settled on tagging the subject like this:

:0
* ^Subject:\/.*
{ SUBJECT = $MATCH }

:0
* ^X-Orig-Subject:\/.*
{ ORIGSUB = $MATCH }

:0
* ^TO_\/(somegroup|another|third)
{
    LISTNAME = $MATCH
    GROUP=""

    :0
    * LISTNAME ?? somegroup
    { GROUP = "SG" }

    :0E
    * LISTNAME ?? another
    { GROUP = "A" }

    :0E
    * LISTNAME ?? third
    { GROUP = "T" }

    :0 fwh
    *$ ! ${GROUP+!}
    *$ ${ORIGSUB+!}
    *$ ! ORIGSUB ?? ($GROUP|$LISTNAME)
    *$ ! SUBJECT ?? (somegroup|another|third)
    | $FORMAIL -I "Subject: [$GROUP]$SUBJECT" -I "X-Orig-Subject: $SUBJECT"

    :0
    $MAILDIR/.aggregate/
}

Which works well enough but might need some explanation.

  • Lines 1-7, extracts the Subject and, if it exists my custom header. It uses procmail’s \/ operator to save the match.
  • Lines 9-12, matches the list name, again extracting the matching portion.
  • Lines 15-25, perform if/else matches against the saved name of the group.
  • Lines 27-32, checks that the group name is set, that our special header is not already set and that the original and current email Subject do not match the group or list name. If all these checks pass, it appends the group name the Subject and tacks on our custom header.
  • Lines 34-35, unconditionally writes the message to a maildir.

Comment


Comment

Commenting is closed for this article.