Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract only the body part of incoming emails using bash

Tags:

bash

maildir

I use offlineimap to fetch the mails into a Maildir folder.

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

So I try to process all mails 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 emails, but how do I get rid of everything that is not the plain body, like signatures, attachements,...?

like image 786
rubo77 Avatar asked Dec 08 '25 08:12

rubo77


1 Answers

As suggested on formail manpage, that worked for me:

cat mailfile | sed -e '1,/^$/ d' 
like image 50
Roberto de Deus Barbosa Murta Avatar answered Dec 09 '25 23:12

Roberto de Deus Barbosa Murta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!