0

Trying to direct email based on a regex match.

I know I can do this:

echo | mail -s test1 me-route2

with this recipe:

* ^To:.*me-route2\@
:0:
/home/me/folder-route2/afile

What I'd like to do is something like this:

* ^To:.*me-(route\d)\@
:0:
/home/me/folder-$MATCH/afile

but I am getting:

procmail: Lock failure on "/home/me/folder-/afile.lock"
jsf80238
  • 127
  • 5

2 Answers2

1

You have the prologue line and the condition in the wrong order. But in addition, Procmail does not recognize the Perlism \d. (See e.g. here for a bit of regex history and arcana.) Try this instead:

:0:
* ^To:.*me-\/route[0-9]
/home/me/folder-$MATCH/afile

or, if you really want to verify that the matching expression is immediately followed by an @ sign,

:0:
* ^To:.*me-\/route[0-9]@
* MATCH ?? ^\/route[0-9]
/home/me/folder-$MATCH/afile

From the input To: reallynotme-route2@example.com, the first condition captures route2@ and the second condition matches up through route2 in order to drop the trailing @ from MATCH.

My suspicion is that no lock file is necessary, so the second colon should be removed, but this depends on what afile is. If it's a regular Berkely mbox file, you should definitely use locking. If it's a Maildir folder, you should not.

You might also want to look into replacing ^To:.*me with ^TO_me -- it will match on Cc: and other pertinent headers as well as a literal To: header, and avoid matching on reallynotme.

tripleee
  • 1,416
  • 3
  • 15
  • 24
0

Procmail doesn't use capture groups like normal regular expressions, at least for MATCH. From the man page:

This variable is assigned to by procmail whenever it is told to extract text from a matching regular expression. It will contain all text matching the regular expression past the `/' token.

So the following should work (untested):

* ^To:.*me-\/route\d

MATCH will then contain whatever matched the regex after the \/, so "route\d".

Sadly this will do possibly unexpected things with an address like me-route2-ab@example.com, with MATCH being "route2". But that may be acceptable to you.

tripleee
  • 1,416
  • 3
  • 15
  • 24
Craig Miskell
  • 4,216
  • 1
  • 16
  • 16
  • Hmm. Did not work (same behavior as before). I think what I'll do is pipe to a program and separate it out there. Thanks, anyway, Craig. – jsf80238 Jan 16 '15 at 21:00
  • That's because `\d` only matches a literal character `d` in Procmail regex. – tripleee Jul 13 '19 at 04:51