Search code examples
emailemail-headersprocmail

Forward a mail, adding the beginning of the body to the email subject


How can I append the beginng of body to the subject of the mail using e.g. procmail? The body of new mail can be erased.


Solution

  • UPDATE extract mime text part only!!!

    Could be done this way:

    :0 c
    * optional rules
    {
        MAILSUBJ=`formail -zcxSubject:`
        MAILBODY=`/usr/local/bin/extract-mime-text.sh`
        NEWSUBJ="$MAILSUBJ $MAILBODY"
    
        :0 fw
        | formail -I"Subject: $NEWSUBJ"
    
        :0
        [email protected]
    }
    

    with the script in /usr/local being something along the lines of

    #!/bin/bash
    
    T=/tmp/tmpmail.$$.$RANDOM
    cat /dev/stdin > $T
    for i in `reformime -i < $T | grep -B1 "content-type: text/plain" | grep section | cut -d: -f2`; do
        reformime -s$i -e < $T | sed -e "s/[ \t]\+/ /g" | tr -d "\n"
    done
    rm $T
    

    That way you can nicely forward messages to some pseudo email address that does SMS forwarding (beginning of text part of mail will end up in subject, as only the subject is forwarded to SMS in many cases)