Question

I have never used procmail before but I believe (from my R&D) that it is likely my best choice to crack my riddle. Our system receives an email, out of which I need 3 values, which are:

  1. either a 4-digit or 5-digit integer from the SUBJECT line. (we will refer to as "N")

  2. email alias from REPLY-TO line (we will refer to as "R")

  3. determine the type of email it is, by which I mean to say a "case" or a "project". (we will refer to as "T") This value would be parsed out of the SUBJECT line.

If any one could help me with that recipe, I would be most appreciative.

The next thing I need to do is:

  1. send these 3 values to a Python script (can I do this directly from procmail? pipe? something else?)
  2. delete the email messages

I need to accept these emails from only 4 domain names, such as:

(@sjobeck.com|@cases.example.com|@messages.example.com|@bounces.example.com)

Last, is to pipe these 3 values in to the second script, and some advice as to the best syntax to do so. Any advice here is most appreciative. Would this be something like this:

this-recipe $N $T $R | second-script.py

Or exactly how would that look? Or is this not a procmail issue and a Python issue? (if it is, that's fine, I'll handle it over there.)

Thanks so much!

Jason

Was it helpful?

Solution

Procmail can extract those values, or you can just pass the whole message to Python on stdin.

Assuming you want the final digits and you require there to be 4 or 5, something like this:

R=`formail -zxReply-to: | sed 's/.*<//;s/>.*//'`
:0
* ^From:.*@(helpicantfindgoogle\.com|searchengineshateme\.net|disabled\.org)\>
* ^Subject:(.*[^0-9])?\/[0-9][0-9][0-9][0-9][0-9]?$
| scriptname.py --reply-to "$R" --number "$MATCH"

This illustrates two different techniques for extracting a header value; the Reply-To header is extracted by invoking formail (this will extract just the email terminus, as per your comment; if you mean something else by "alias" then please define it properly) while the trailing 4- or 5-number integer from the Subject is grabbed my matching it in the condition with the special operator \/.

Update: Added an additional condition to only process email where the From: header indicates a sender in one of the domains helpicantfindgoogle.com, searchengineshateme.net, or disabled.org.

As implied by the pipe action, your script will be able to read the triggering message on its standard input, but if you don't need it, just don't read standard input.

If delivery is successful, Procmail will stop processing when this recipe finishes. Thus you should not need to explicitly discard a matching message. (If you want to keep going, use :0c instead of just :0.)

As an efficiency tweak (if you receive a lot of email, and only a small fraction of it needs to be passed to this script, for example) you might want to refactor to only extract the Reply-To: when the conditions match.

:0
* ^From:.*@(helpicantfindgoogle.com|searchengineshateme\.net|disabled\.org)\>
* ^Subject:(.*[^0-9])?\/[0-9][0-9][0-9][0-9][0-9]?$
{
  R=`formail -zxReply-To: | sed 's/.*<//;s/>.*//'`
  :0
  | scriptname.py --reply-to "$R" --number "$MATCH"
}

The block (the stuff between { and }) will only be entered when both the conditions are met. The extraction of the number from the Subject: header into $MATCH works as before; if the From: condition matched and the Subject: condition matched, the extracted number will be in $MATCH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top