【问题标题】:Extract only the body part of incoming emails using bash使用 bash 仅提取传入电子邮件的正文部分
【发布时间】:2021-02-03 06:42:18
【问题描述】:

我使用offlineimap 将邮件提取到 Maildir 文件夹中。

我想自动解析 Maildir 文件夹中所有新收到的电子邮件,并仅将“发件人”、“主题”和“正文”作为即时消息发送到其他地方。

所以我尝试处理所有邮件

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

这对于一些简单的电子邮件已经很好用了,但是我如何摆脱所有不是普通正文的东西,比如签名、附件……?

【问题讨论】:

  • 使用能够理解邮件的工具,而不是通过简单的字符串处理来破解它?
  • 如果 bash 命令行有一个简单的工具,我愿意吗?
  • 你到底想在这里做什么? “阅读”您的电子邮件?
  • 运气好吗?我正在寻找类似的东西。

标签: bash maildir


【解决方案1】:

按照 formail 手册页的建议,这对我有用:

cat mailfile | sed -e '1,/^$/ d' 

【讨论】:

    【解决方案2】:

    正如 cmets 中的回答,procmail 包中的 formail 似乎做得很好:

    $ 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"
    

    致谢:@glennjackman

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 1970-01-01
      • 2016-04-12
      • 2011-08-24
      • 2020-07-14
      相关资源
      最近更新 更多