Your assignment is a useless use of backticks. You want simply
ATTACH="/home/foo/attachments/camera"
But if this is a static string, putting it in a variable doesn't really buy you anything.
To actually extract a subject, the thing after the \/
needs to be a regex which matches the part of the subject you want.
:0
* ^To:.*devicemail@mydomain\.com
* ^Subject:[ ]*\/[^ ].+
| munpack -s "$MATCH" -q -C "$ATTACH"
The regex [ ]*
before the \/
token skips any leading whitespace (inside the brackets, you need a tab and a space, to match any sequence of either). The [^ ]
matches the first non-whitespace character (not space, and not tab) of the field, and we then capture the rest of the line with .+
Notice also the proper quoting of the variables. Generally, you should put variables in double quotes unless you specifically want the shell to perform whitespace tokenization and wildcard expansion on the value.
I removed the c
flag after :0
because I'm guessing you don't actually want Procmail to also do other things with this message (but that's speculative; if I'm wrong, by all means put it back).
If indeed munpack
doesn't support -s
then maybe something like this:
:0
* ^To:.*devicemail@mydomain\.com
* ^Subject:[ ]*\/[^ ].+
| extract "$ATTACH/$MATCH"
where extract
is a script something like
#!/bin/sh
dest=$1
mkdir "$dest.tmp"
munpack -q -C "$dest.tmp"
set -- "$dest.tmp"/*
test -e "$1" || { rmdir "$dest.tmp"; echo "$0: $dest: nothing extracted" >&2; exit 2;}
case $# in 1) mv "$1" "$dest"; rmdir "$dest.tmp"; exit 0;; esac
echo "$0: $dest.tmp contains multiple files; halp!"; exit 1
This extracts into a directory, then if the directory contains a single file, moves that to the final destination. If nothing was extracted, do nothing. If there is more than one extracted file, bail out with an error.
You can certainly pipe the entire message to Python instead, but pushing the functionality into Python does not seem to be necessary or useful here. (Again, you might have additional context which tips the scales the other way, but we don't know enough from your question to conclude that that is the case here.)