Extract only the body part of incoming emails using bash

不想你离开。 提交于 2019-12-24 00:25:24

问题


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


回答1:


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
 test@atleticomadridhistory.com

$ cat test.eml | formail -x Subject
 hello

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

Credits: @glennjackman



来源:https://stackoverflow.com/questions/36168224/extract-only-the-body-part-of-incoming-emails-using-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!