简体   繁体   中英

Munpack and Procmail - getting subject line

Im saving attachments in emails in a directory with munpack. This works but poses a problem for my application.

The email is generated form a device and it ads its UID to the subject line. I need to somehow get the subject line from the email and either save the image as that, or add the subject line to the .desc file that munpack creates when saving the file.

This is my current procmailrc rule:

ATTACH=`echo /home/foo/attachments/camera`
:0 c
* ^To:.*devicemail@mydomain.com
* ^Subject:[^   ]*\/:
| munpack -s $MATCH -q -C $ATTACH

-s does not seem to be a valid option anymore.

Not sure if that Subject bit actualy works

OR could I send the email file to a python script to find the subject line?

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM