0

I have a procmailrc file that is collecting some variables from mail headers, then echo'ing those variables into the message body. Like this:

:0bfw 
| echo Subject_ "${SUBJECT_}" ; echo From_ "${FROM_}" ; echo To_ "${TO_}" ; echo CC_ "${CC_}" ; echo "" ; cat

This works okay in general. It is correctly echo'ing what I want it to echo. But it's echoing those variables into a separate part of the message, like this:

Content-Type: multipart/alternative; boundary=001a11c1367a19ff420508126c21


Subject_  Subject variable is here
From_  variable@whatever
To_  variable@whatever
CC_ 

--001a11c1367a19ff420508126c21
Content-Type: text/plain; charset=UTF-8

The rest of the message body is here, in a separate message part.

How can I make procmail echo those variables into the same part of the message as the rest of the email body?

1 Answer 1

0

This is slightly tricky because not all messages are MIME messages, and not all MIME messages are multipart messages. The following is thus probably a bit too simplistic, but works in my limited tests.

:0fbw
* ^MIME-version:
* ^Content-type:[   ]*multipart/[^-a-z0-9_]*;[  ]*boundary="?\/[^   "]*
| awk "!q&&/^--$MATCH/{h=1}h&&/^$/{h=0;p=1}\
    !q&&p&&!/^$/{print \"Subject_ $SUBJECT_\"; print \"From_ $FROM\";\
        print \"To_ $TO\"; print \"CC_ $CC\"; print \"\"; p=0; q=1 }1"
:0Efbw
| echo blah blah  # Your original attempt here

The Awk script locates the first MIME boundary, then the first empty line after that, then inserts the snippet, and sets a couple of state variables to prevent further processing. Unfortunately, the script is slightly brittle; it will fail if one of the extracted values contains unpaired double quotes, and process paired double quotes incorrectly.

Furthermore, this will only work correctly if the first body part within the multipart is a text part; it will fail similarly to your current case if you have nested multiparts (top-level message multipart/related containing a multipart/alternative structure, for example). It can be extended to cover more cases -- a simple tweak would be to skip up to the first Content-Type: text/plain instead --, but at some point, it will make more sense to do the MIME manipulation in a properly MIME-aware tool (a simple Python script, for example).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .