Is there a way with procmail (or something else?) to search for email where the from: and to: fields both contain the same address? And can it be done without needing to hardcode each and every email address in a recipe?

Basically I'm trying to find emails where the sender sends the mail to their own address then bcc's everyone else. Normally I use notmuch and I'm happy with that but it can't do what I'm trying to achieve here. notmuch can work together with procmail though so I was hoping someone who knows procmail really well could help and thus save me from needing to learn a whole new filtering system to do this one thing.

  • should you not search into logs rather than procmail? Oct 28 '16 at 19:26

This is possible, though it requires some rather obscure Procmail features, and of course, you need to understand what you are achieving.

*   ^From:[     ]*\/[^  ].*
* $ ^To:[   ]*$\MATCH
{ ... actions ... }

The \/ capture operator collects the matching string into the special variable MATCH. On the next line, we search for the same string in the To: header. Where $MATCH contains the captured string, $\MATCH contains a regex where any regex special characters in the string have been escaped so as to match literally. The $ modifier is required on the recipe in order to allow interpolation of Procmail variables into the regex.

This uses the usual [ ]* (space or tab, zero or more) to skip over whitespace after the colon, then grabs everything starting from the first non-whitespace character.

In practice, this means that the From: and To: headers have to have identical contents. If the sender puts in a different "real name" (which technically is just a comment), this recipe will fail to match. For example,

From: Myself <me@example.net>
To: Everyone <me@example.net>

Here, the email terminus is identical, but the whole field is different. If you need to cope with this scenario, maybe change the regex to match up through the first < before the \/ but this will obviously depend on the precise strings you need to match; there are many possible variations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.