2

I am trying to redirect emails that match a particular pattern to a shell script which will create files containing the texts, with datestamped filenames.

First, here is the routine from .procmailrc that hands the emails off to the script:

:0c:
* Subject: ^Ingest_q.*
| /home/myname/procmail/process

and here is the script 'process':

#!/bin/bash

DATE=`date +%F_%N`
FILE=/home/myname/procmail/${DATE}_email.txt

while read line
        do
            echo "$line" 1>>"$FILE";
        done

I have gotten very frustrated with this because I can pipe text to this script on the command line and it works fine:

mybox-248: echo 'foo' | process
mybox-249: ls
2013-07-31_856743000_email.txt  process

The file contains the word 'foo.'

I have been trying to get an email text to get output as a date-stamped file for hours now, and nothing has worked.

(I've also turned logging on in my .procmailrc and that isn't working either -- I'm not trying to ask a second question by mentioning that, just wondering if that might provide some hint as to what I might be doing wrong ...).

Thanks,

GB

Glenn Becker
  • 21
  • 1
  • 3
  • 2
    If you can't turn on logging I'd suspect that your `.procmailrc` is never read and thus the recipe is never triggered. Try [debugging](http://partmaps.org/era/mail/procmail-debug.html) your procmail setup. – Ansgar Wiechers Jul 31 '13 at 17:43
  • Thank you, Ansgar -- that's a helpful page. I'm working through it. – Glenn Becker Jul 31 '13 at 19:01

1 Answers1

2

Quoting your attempt:

:0c:
* Subject: ^Ingest_q.*
| /home/myname/procmail/process

The regex is wrong, ^ only matches at beginning of line, so it cannot occur after Subject:. Try this instead.

:0c:process.lock
* ^Subject: Ingest_q
| /home/myname/procmail/process

I also specified a named lockfile; I do not believe Procmail can infer a lock file name from just a script name. As you might have multiple email messages being delivered at the same time, and you don't want their logging intermingled in the log file, using a lock file is required here.

Finally, the trailing .* in the regex is completely redundant, so I removed it.

(The olde Procmail mini-FAQ also addresses both of these issues.)

I realize your recipe is probably just a quick test before you start on something bigger, but the entire recipe invoking the process script can be completely replaced by something like

MAILDIR=/home/myname/procmail
DATE=`date +%F_%N`
:0c:
${DATE}_email.txt

This will generate Berkeley mbox format, i.e. each message should have a From_ pseudo-header before the real headers; if you are not sure whether this is already the case, you should probably use procmail -Yf- to make sure to make it so (otherwise there is really no way to tell where one message ends and another begins; this applies both to your original solution, and this replacement).

Because Procmail sees the file name you are delivering to, it can infer a lockfile name now, as a minor bonus.

Using MAILDIR to specify the directory is the conventional way to do this, but you can specify a complete path to an mbox file if you prefer, of course.

tripleee
  • 175,061
  • 34
  • 275
  • 318