Extract only the body part of incoming emails using bash
Asked Answered
D

2

3

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,...?

Dyann answered 23/3, 2016 at 1:24 Comment(14)
Use a tool that understands mail instead of hacking it up with simple string processing?Affenpinscher
I would, if there is a simple tool for the bash commandline?Dyann
What are you actually trying to do here? "Read" your email?Affenpinscher
Find an MDA you can configure to run commands on email as it gets delivered?Affenpinscher
I use offlineimap to fetch the mails into a Maildir folder at the momentDyann
Check out formail to extract email headersRici
@glennjackman: How do you install formail? it is not in the debian repositoriesDyann
packages.debian.org/…Rici
So i have to install the whole procmail? Can't i just get the formail bin somehow?Dyann
Any luck with that? I'm looking for similar thing.Squall
There's so very much wrong here. Start with mywiki.wooledge.org/ParsingLsHoney
...and then why in the world would you cat a file over and over, instead of just reading it in one pass and parsing out the parts you want?Honey
(...though if we needed MIME support, personally, I'd probably use Python's standard-library tooling rather than trying to reinvent the wheel).Honey
I would be happy, of you put this in an answer with a solution to useDyann
C
4

As suggested on formail manpage, that worked for me:

cat mailfile | sed -e '1,/^$/ d' 
Calvo answered 2/2, 2021 at 22:42 Comment(0)
S
2

As answered in the comments, formail from the procmail package seems to do the job nicely:

$ sudo apt-get install procmail

$ cat test.eml | formail -x To
 [email protected]

$ cat test.eml | formail -x Subject
 hello

$ cat test.eml | formail -x Content
 multipart/alternative; boundary="f403043eea78e8658a0554677278"

Credits: @glennjackman

Solangesolano answered 16/7, 2017 at 4:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.