1

Here is what I was thinking of:

I want to create a Raspberry Pi Picture-frame (DPF) for my grandma, but with basic email support. Emails can be sent by six family members and will be received daily by fetchmail/procmail. This already works!

In /home/pi/Mail/ I now have three files (one for each email including attachments):

  • msg.nTIB
  • msg.pTIB
  • msg.oTIB

What I want to do now is extract SENDER, SUBJECT(, TEXT) and ATTACHMENT from each file, to rename the attachment-image and add the text with imagemagick (not yet included).

for file in msg.*; do
    export SENDER=`tee $file | formail -zxFrom: -zxReply-To:|cut -fs- -d'<'|cut -f1 -d'>'`
    SUBJECT = `/bin/cat $file | formail -zxSubject:`

    if grep -qxis $SENDER /home/pi/Mail/.markus; then
        SENDER2=Markus
    fi
    if grep -qxis $SENDER /home/pi/Mail/.someone; then
        SENDER2=Someone
    fi

    /bin/mkdir -p /home/pi/Mail/$SENDER2 >/dev/null 
    munpack -C /home/pi/Mail/$SENDER2 $file
    /bin/cat $file
    /bin/rm $file
done

The grep and munpack part works fine, but the extraction of SENDER and SUBJECT does not work when I run the sh-file. It just looks like it froze. I can only cancel it with ctrl-c!

I don't know why, when I include line 2 and 3 in my .procmailrc it does work without problems.

I mean that this is a somehow basic task and no wizardry, but I couldn't find anything helpful with google :/

Any help is appreciated! Maybe you have got another solution. I'm totally not an expert in Linux, so maybe keep it a bit simple :)

Kevin Panko
  • 7,466
Markus
  • 269

1 Answers1

1

Both the cat and the tee are useless; the tee doubly so, and the reason for (what you perceive as) the script freezing. It requires a file name argument, as the file to copy its input to, and a stream of text or data on standard input, which of course it's not receiving, so it sits there.

Additionally, you should generally quote your variables.

The SENDER extraction is kind of problematic. Do you really want the variable to contain two strings? Usually, you could simply do formail -rtzxTo: <"$file" and obtain a sane result. (The -t option has some obscure semantics but for this sort of purpose, it probably does the right thing.) That also does away with the pesky cuts to obtain just the email terminus (because formail in reply generation mode already does that for you).

Finally, sh syntax does not allow for whitespace around the equals sign in assignments.

for file in msg.*; do
    SENDER=$(formail -zxFrom: -zxReply-To: <"$file" |
        cut -fs- -d'<'|cut -f1 -d'>')
    SUBJECT=$(formail -zxSubject: <"$file")

    if grep -qxis "$SENDER" /home/pi/Mail/.markus; then
        SENDER2=Markus
    elif grep -qxis "$SENDER" /home/pi/Mail/.someone; then
        SENDER2=Someone
    fi

    mkdir -p /home/pi/Mail/"$SENDER2"
    munpack -C /home/pi/Mail/"$SENDER2" "$file"
    cat "$file"
    rm "$file"
done

I also removed the full paths from command names (you should simply make sure your PATH is sane instead) and switched from `obsolescent` process substitution syntax to $(modern). mkdir -p runs quietly, so I removed the output redirection from that.

The complex grep could be replaced with a simple mapping, something like this:

SENDER2=$(awk -v who="$SENDER" '
    $1 == who { print $2; x=1 } END { exit 1-x }' <<'________HERE'
        msmith@dmel.example.com    Markus
        someone@there.example.net  Someone
________HERE

This makes assumptions about what you have in the files you are currently grepping from, but you should get the idea. This still doesn't solve how to set SENDER2 if you don't have the sender on file; but perhaps the script should simply reject those files altogether for security reasons?

tripleee
  • 3,308
  • 5
  • 36
  • 35