Search code examples
linuxsedawkprocmail

print out variable in procmail


I have a Procmail recipe to filter incoming mail such as below:

:0
*^Subject:.*(test)
* ? egrep -is -f /root/Procmail/whitelist.txt
{            
    :0 fwb
    | formail -I ""
    
    :0
    myfolder/
}

The above recipe function is to filter out the body content of the email and forward that mail to myfolder. The problem is I have a variable that I want to put inside the body.

FROM_=`formail -c -x"From " \
     | expand | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g' \
     | awk '{ print $1 }'`

    SUBJ_=`formail -c -x"Subject:" \
     | expand \
     | sed -e 's/  */ /g' \
     | sed -e 's/^[ ]*//g' -e 's/[ ]*$//g'`

This email body (together with the variable) should be forwarded to myfolder.

I've tried to echo the variable like this but still no use.

:0 fwb
echo "${SUBJ_}"
echo "{FROM_}"

Is the something wrong with my recipe?


Solution

  • You need to pipe into the shell script. An action without a prefix saves to a folder named "echo", in your case.

    You were also lacking a dollar sign on the ${FROM_} variable.

    :0 fwb
    | ( echo "${SUBJ_}";  echo "${FROM_}" )
    

    Your assignments could probably be optimized quite a bit. Piping sed to sed or awk is rarely necessary; if sed cannot handle what you want, then let awk do it all.

    FROM_=`formail -c -x"From " \
     | expand \
     | awk '{ gsub (/^[ ]*|[ ]*$/,""); print $1 }'`
    
    SUBJ_=`formail -c -x"Subject:" \
     | expand \
     | sed -e 's/  */ /g' -e 's/^[ ]*//g' -e 's/[ ]*$//g'`
    

    (Not sure why you would need expand in there either, but I left it in just in case.)