linux - munpack(MIME 编码的邮件消息)的替代方案是什么?

标签 linux email-attachments incoming-mail

我发现了问题,Munpack 会以防万一我的邮箱文本文件中有一封邮件。如果我有更多邮件,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

关于linux - munpack(MIME 编码的邮件消息)的替代方案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741486/

相关文章:

c# - 发送到多个电子邮件地址时,ASP.NET C# 电子邮件附件最终变为 0 字节

Python 脚本作为 cron 作业 - 如何将输出格式化为带有附件的电子邮件?

java - 如何将邮件从 gmail 路由到 mailgun?

javascript - 如何使用 gmail api javascript 获取已读和未读邮件列表

python - 乌尔都语字体的逆向渲染

linux - 将大量文件分发到较小的组的脚本

linux - 使用 ImageMagick : too many exceptions and blinking problem 创建 GIF 图像时出错

python - 定影器替代品?我想知道访问文件的所有进程

python - IMAP 如何保存**所有**附件

java - 使用 java 使用 selenium webdriver 从 Gmail 收件箱打开邮件