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

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.

  • 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 at 4:51

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.