How to use procmail to save email body with email subject as the filename?

1.2k views Asked by At

I am using procmail to save email body, something like this:

:0: tmpProcmail.lock
* ^From:.*(SOME EMAIL).*
* ^Subject: SOME SUBJECT.*
| cat > /SOME DIRECTORY/$(date +\%Y\%m\%d).txt

I wonder if I can use the whole subject as my filename instead of date.

1

There are 1 answers

2
tripleee On BEST ANSWER

It doesn't strike me as a particularly good idea, but it's not hard to do. Just use the \/ capturing token to get the matching text into $MATCH.

:0:
* ^From:.*(SOME EMAIL)
* ^Subject: \/SOME SUBJECT.*
| cat > "/SOME DIRECTORY/$MATCH.txt"

You say you save the body, but your recipe doesn't do that; it saves the whole message. (Add a b flag if you want only the body in the file.)

Also note how I omitted the named lock file (it's more efficient to let Procmail figure out a lock file name in this scenario ... though locking probably deosn't matter all that much if you overwrite the file anyway) and the redundant .* in the From: regex. (It's not redundant in the Subject because you want to capture the entire header.)

This will of course fail if the Subject contains a slash and you don't have a corresponding directory name on disk.

If you don't want to overwrite, the default is to append, so no cat is required or useful.

:0:
* ^From:.*(SOME EMAIL)
* ^Subject: \/SOME SUBJECT.*
/SOME DIRECTORY/$MATCH.txt