Score:0

procmail rewrite subject line if email recipient user fails match test

in flag

I have a local procmail user "bob".

If an email arrives and matches [email protected], then no action should be taken.

However, if an email arrives and is [anything_else]@domain.com then I would like to rewrite the subject line to insert "[spam]" at the front of the subject line.

So it is a reverse match on "bob@" (match on anything other than bob@) and we preserve the original subject line, just prepending "[spam]" to the front of it.

How can I do that with Procmail?

Score:1
ar flag

Your requirements are rather unclear. Procmail can match on any string, but do you really mean to look for the string anywhere in the message? And do you really mean to ignore messages without domain.com in them for the purpose of this rule?

:0 HB
* ! ()\<bob@domain\.com\>
* @domain\.com\>
{
    :0fhw
    * ^Subject:\/.+
    | formail -I"Subject: [spam]$MATCH"
}

The \< and \> word boundaries prevent matching on substrings like tombob or subdomain.complete.org, and the empty parentheses are a hack because Procmail is weird about regexes which start with a backslash.

If you mean you want to look only in the headers, drop the HB from the first colon line (you could leave the H but that's the default if you don't put any flags). If you want to match a specific header, spell it out. If you want to examine the recipient specifically, the ^TO_ macro lets you do that in a number of different headers (To:, Cc:, etc) easily.

:0fhw
* ! ^TO_bob@domain\.com\>
* ^TO_[^<>@ ]+@domain\.com\>
* ^Subject:\/.+
| formail -I"Subject: [spam]$MATCH"

Because we dropped the HB flags, we can combine what was previously two recipes with different flags. The second ^TO_ checks for any address including bob, which however was already excluded by the previous negated condition.

This still has some corner cases which might need to be explored further; if you can edit your question to clarify it, perhaps I can update this to cover more than the basics.

SMTP doesn't require the recipient to be spelled out in the headers, though. The classic case is Bcc: which is also essentially the mechanism used by many mailing lists. Perhaps your MTA will spell out the recipient in Delivered-To: but properly speaking, this type of filter is better implemented in the MTA instead.

user227963 avatar
in flag
No, I do not mean to look for the string anywhere in the message - just in the TO field ... so if TO matches [email protected], no action taken. BUT, if the TO field is anything else, then I would like to rewrite the subject line with [SPAM] at the beginning. Make sense ?
tripleee avatar
ar flag
Anything at all, or any other recipient in this specific domain, like your question says? The second example already shows you exactly how to do this.
user227963 avatar
in flag
I tested the second example and it works very nicely - although there is one thing ... if the subject line is totally blank it stays blank - it does not get rewritten ...
tripleee avatar
ar flag
You can change the last `.+` in the Subject: condition to `.*` to also capture empty Subject: lines. (Nobody should be sending messages with empty subjects, though.)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.