Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently my procmail recipes are as below:

 FROM_=`formail -c -x"From "| awk '{ print $1 }'`

 SUBJ_=`formail -c -x"Subject:" | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

  :0 fwh
  | formail | (echo "$FROM_"; echo "$SUBJ_";)

  :0 fwb
  | formail -I ""


My procmail recipe will only filter out the emails that have number "60" in their subject. After that formail will execute taking out the sender's email address and its subject before forwarding it together with its content to the "myemail" directory.

Can someone help me to construct a procmail recipe to filter incoming mails into different directories if there are multiple "60" seperated by commas in the subjects?

(For example,the email with subject 6010,6011,6012,.. the 6010 will go into 6010 folder,6011 will go into 6011 and so on.)

The numbers in the subject are phone numbers, there can be a large number of different ones so enumerating all the possible matches is not really an option.

share|improve this question

If your mailboxes are directories, you can list all of them in a single delivery action. Then it all becomes as simple as

* ^Subject:.*60
`formail -zxSubject: | egrep -o 60[0-9]+ | sed 's%$%/%'`

The script in the backticks will output something like 60123456789/ (newline) 609876543210/ (newline) -- as many as there are phone numbers; each one is treated as a folder name, and the message is delivered to all of those folders.

(I'm sure the egrep | sed could be refactored into a single sed script. Adding a slash after each folder name is necessary for Procmail to treat the delivery action as a directory delivery. You cannot deliver into multiple mbox folders at the same time.)

My original answer follows below the line.

What you are asking about is fairly tricky to do in Procmail. There is no facility for looping over a set of matches. A somewhat desperate workaround would be to collect the matches into a variable, then INCLUDERC= to a different recipe file which will process one value from the beginning of the variable, then SWITCHRC= to itself again while there are values left in the variable. It doesn't have to be very complicated but it's decidedly hackish, to the point where you have to ask if Procmail is really the right tool for the job.

Of course, there is also the brute-force option; create a sequence of recipes to handle each possible 60xx pattern.

* ^Subject:.*6000

etc for 6001, 6002. This presupposes that you have a limited number of these, or if you need to handle each case from 00 to 99, perhaps generate the recipes with a simple script.

perl -e 'printf ("%02i\n", $_) for 0..99' |
while read i; do
    cat <<____HERE
    * ^Subject:.*60$i
done >60xx.rc
share|improve this answer
i don't think that this is possible.Because actually the information on the subject represent the phone number of the customers.So the number that available in the "Subject" are very random.The only constant between these numbers are 60 which are country code.Perhaps I can use the sed / awk in the recipe to filter this? – user119720 Jan 2 '13 at 2:53
The problem is not the extraction, but the handling of multiple matches. For a single match, grab it with * ^Subject:.*\/60[0-9]+ and you will have the number in $MATCH. – tripleee Jan 2 '13 at 5:36
Updated the answer. Hope this helps. – tripleee Jan 3 '13 at 7:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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