簡體   English   中英

munpack(MIME編碼的郵件)的替代方法是什么?

[英]What is the alternative of munpack(MIME-encoded mail message)?

我發現了問題,如果萬一我的郵箱文本文件中有一封郵件,Munpack將無法正常工作。

有人能告訴我有關替代品的事情嗎?

我有同樣的問題。 我嘗試使用標准的Unix工具來幫助自己。 我制作了一個小的bash腳本,非常適合我的需求。 我不是Shell程序員,一個有能力的程序員可能會以不同的方式來做。 外觀和隨意更改/改進腳本。 歡迎評論! (以改善我的bash編程:

    #!/bin/bash
    #Script extracts attachments out of a "ASCII" email file.
    # Using grep, head, tail and openssl.
    # Start of a attachment looks like this in the file:
    # 
    #--Part_0_1323333549.1457526416376
    #Content-Type: application/octet-stream;
    #   name="testfile.zip"
    #Content-Transfer-Encoding: base64
    #Content-Disposition: attachment; filename="testfile.zip"
    #
    #UEsfBBQACAAIAAg4fEYAAAAAAAdAAAAAAAAeAAAAQWJsZWhugW5nZW5fQ0RDXzIwMTUtMDMtMjgu
    #...
    #...
    #
    # End of a attachment looks like this: 
    #AAAAAAAAAEFibGVobnVuZ2VuX0NEQ18yMDE1LTAzLTI3LmNzdlBLBQYAAAAAAQABAEwAAACrTwIA
    #AAA=
    #
    #--Part_0_1323333549.1457526416376
    #

    timestamp=$(date +20%y-%m-%d_%H-%M-%S)
    mailpath=~/mail
    mailfile=$mailpath/mails
    tmpfile=$mailpath/tmpfile.txt
    tmpfile2=$mailpath/tmpfile2.txt
    outfile=$mailpath/outfile.txt

    # fetchmail (MTA=Mail Tranfer Agent) connects to mail-server and routes mails to procmail (MDA=Mail Delivery Agent)
    # Configuration ~/.fetchmailrc und ~/.procmailrc
    fetchmail

    cp $mailfile $tmpfile

    # Get NB of all attachments in one or n emails. Option -c in grep (all emails are in $mailfile resp. $tmpfile!)
    # to set loops
    nbattach=$(grep -ci 'content-disposition: attachment; filename=' $tmpfile)

    i=1
    while [ $i -le $nbattach ]
    do

      echo $i. run
      #get filename of 1. attachments in remaining $tmpfile --> option -m 1 in grep!
      filename=$(grep -ni 'content-disposition: attachment; filename=' $tmpfile -m 1 | cut -d ';' -f 2 | cut -d '=' -f 2 | sed 's/"//g')
      echo filename: $filename

      #get startline of 1. attachments in remaining $tmpfile--> option -m 1 in grep
      startline=$(grep -ni 'content-transfer-encoding: base64' $tmpfile -m 1 | cut -d : -f 1)

      #attachment starts after a blank line
      let startline=$startline+1

      #keep tail in $tmpfile
      tail -n +$startline $tmpfile > $tmpfile2
      mv $tmpfile2 $tmpfile

      #get endline of the 1. attachment in remaining $tmpfile
      #attachment ends with a blank line and a string that begins with '--'
      endline=$(grep -n '^--' $tmpfile -m 1 | cut -d ':' -f 1)
      let endline=$endline-1

      #keep head and decode attachment with base64 via openssl.
      head -n $endline $tmpfile > $outfile
      openssl enc -d -base64 -in $outfile -out ~/mail/attachments/${timestamp}_${i}_${filename}

      # keep tail for further attachemnts
      let endline=$endline+2
      tail -n +$endline $tmpfile > $tmpfile2
      mv $tmpfile2 $tmpfile

      let i=$i+1
    done
    rm $tmpfile $outfile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM