Extract only body part of incoming emails using bash

I am using offlineimap to extract emails to the Maildir folder.

I want to automatically analyze all new incoming messages in the Maildir folder and send only "from", "subject" and "body" as an instant message somewhere else.

So, I'm trying to process all emails with

 MPATH=~/Mail if [ -n "$(ls "$MPATH/INBOX/new/")" ]; then for f in "$MPATH/INBOX/new/"*; do SUB="$(cat "$f"|grep '^Subject' | head -n1 | sed "s/Subject: //g")" FROM="$(cat "$f" | grep '^From' | head -n1 | head -n 1|sed "s/From: //g")" BODY="$(cat "$f"|sed -e '1,/Content-Transfer-Encoding/d')" MESS="$FROM: $SUB$BODY" echo $f echo "$MESS" mv "$f" "$MPATH/INBOX/cur/" done fi 

This already works fine for some simple letters, but how can I get rid of everything that is not a simple body, for example, signatures, attachments, ...?

+2
source share
1 answer

As pointed out in the comments, the formail from the procmail package seems to do the job fine:

 $ sudo apt-get install procmail $ cat test.eml | formail -x To test@atleticomadridhistory.com $ cat test.eml | formail -x Subject hello $ cat test.eml | formail -x Content multipart/alternative; boundary="f403043eea78e8658a0554677278" 

Credits: @glennjackman

0
source

All Articles