Re: extract attachments from multiple mails
authorJameson Graef Rollins <jrollins@finestructure.net>
Mon, 25 Jun 2012 17:14:53 +0000 (10:14 +1700)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:48 +0000 (09:47 -0800)
3d/10ffb433cc4138b64333c5dc0588604a156b17 [new file with mode: 0644]

diff --git a/3d/10ffb433cc4138b64333c5dc0588604a156b17 b/3d/10ffb433cc4138b64333c5dc0588604a156b17
new file mode 100644 (file)
index 0000000..ffee513
--- /dev/null
@@ -0,0 +1,145 @@
+Return-Path: <jrollins@finestructure.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 3CBF1431FB6\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 10:15:04 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.29\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.29 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3, T_MIME_NO_TEXT=0.01] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id LiBqWFTuEGgn for <notmuch@notmuchmail.org>;\r
+       Mon, 25 Jun 2012 10:15:02 -0700 (PDT)\r
+Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
+       [131.215.239.19])\r
+       by olra.theworths.org (Postfix) with ESMTP id E8C61431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 10:15:01 -0700 (PDT)\r
+Received: from fire-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
+       by fire-doxen-postvirus (Postfix) with ESMTP id 551BE2E50E3C;\r
+       Mon, 25 Jun 2012 10:14:59 -0700 (PDT)\r
+X-Spam-Scanned: at Caltech-IMSS on fire-doxen by amavisd-new\r
+Received: from finestructure.net (m8.ligo.caltech.edu [131.215.115.108])\r
+       (Authenticated sender: jrollins)\r
+       by fire-doxen-submit (Postfix) with ESMTP id 135972E50E7B;\r
+       Mon, 25 Jun 2012 10:14:56 -0700 (PDT)\r
+Received: by finestructure.net (Postfix, from userid 1000)\r
+       id AFC976DA; Mon, 25 Jun 2012 10:14:55 -0700 (PDT)\r
+From: Jameson Graef Rollins <jrollins@finestructure.net>\r
+To: David Belohrad <david@belohrad.ch>, notmuch@notmuchmail.org\r
+Subject: Re: extract attachments from multiple mails\r
+In-Reply-To: <uaxwr2vzkdh.fsf@beesknees.cern.ch>\r
+References: <uaxwr2vzkdh.fsf@beesknees.cern.ch>\r
+User-Agent: Notmuch/0.13.2+54~ga0426dc (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Mon, 25 Jun 2012 10:14:53 -0700\r
+Message-ID: <87obo72u0i.fsf@servo.finestructure.net>\r
+MIME-Version: 1.0\r
+Content-Type: multipart/signed; boundary="=-=-=";\r
+       micalg=pgp-sha256; protocol="application/pgp-signature"\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 25 Jun 2012 17:15:04 -0000\r
+\r
+--=-=-=\r
+\r
+On Mon, Jun 25 2012, David Belohrad <david@belohrad.ch> wrote:\r
+> someone can give an advice? I have many emails containing\r
+> attachment. This is typically an output of copy-machine, which fragments\r
+> a scan into multiple attachments.\r
+>\r
+> I'd like to extract those attached files in a one batch into a specific\r
+> directory. Is there any way how to programmatically fetch those files?\r
+\r
+notmuch show has a --part option for outputting a single part from a\r
+MIME message.  Unfortunately there's currently no clean way to determine\r
+the number of parts in a message.  But sort of hackily, you could do\r
+something like:\r
+\r
+for id in $(notmuch search --output=messages tag:files-to-extract); do\r
+    for part in $(seq 1 10); do\r
+        notmuch show --part=$part  --format=raw $id > $id.$part\r
+    done\r
+done\r
+\r
+That will also save any multipart parts, which aren't really that\r
+useful, so you'll have to sort through them.\r
+\r
+You can make something much cleaner with python, using the notmuch and\r
+email python bindings:\r
+\r
+http://packages.python.org/notmuch/\r
+http://docs.python.org/library/email-examples.html\r
+\r
+I hacked up something simple below that will extract parts from messages\r
+matching a search term into the current directory (tested).\r
+\r
+hth.\r
+\r
+jamie.\r
+\r
+\r
+#!/usr/bin/env python\r
+\r
+import subprocess\r
+import sys\r
+import os\r
+import notmuch\r
+import email\r
+import errno\r
+import mimetypes\r
+\r
+dbpath = subprocess.check_output(['notmuch', 'config', 'get', 'database.path']).strip()\r
+db = notmuch.Database(dbpath)\r
+query = notmuch.Query(db, sys.argv[1])\r
+for msg in query.search_messages():\r
+    with open(msg.get_filename(), 'r') as f:\r
+        msg = email.message_from_file(f)\r
+    counter = 1\r
+    for part in msg.walk():\r
+        if part.get_content_maintype() == 'multipart': continue\r
+        filename = part.get_filename()\r
+        if not filename:\r
+            ext = mimetypes.guess_extension(part.get_content_type())\r
+        if not ext:\r
+            ext = '.bin'\r
+        filename = 'part-%03d%s' % (counter, ext)\r
+        counter += 1\r
+        print filename\r
+        with open(filename, 'wb') as f:\r
+            f.write(part.get_payload(decode=True))\r
+\r
+--=-=-=\r
+Content-Type: application/pgp-signature\r
+\r
+-----BEGIN PGP SIGNATURE-----\r
+Version: GnuPG v1.4.12 (GNU/Linux)\r
+\r
+iQIcBAEBCAAGBQJP6JyNAAoJEO00zqvie6q8CmkP/0mZo91WaNASXNBL6+0OcvCT\r
+eH9zyTd+veG6ctnGrFBqNyDlnijxCkSfIM8duQG/IrCd/MyUvb2rdQLa3SIxshaK\r
+EqXVud8QyUyAH2e6fFR4NVsdLEEvJKOehwvYA6ZszUPTREprhuVKYxQUu1yjz6P5\r
+0n91Wwn51nV5mCG154qSr3zR1ODF2ah97eol3ZWH0YccVlRpc3rs1nhpFDFkIZQS\r
+4ivK4cnf47Ffp68up7Uo3msQcj4D0kXvtMG8cr2Bq5ifzq57nQuh72EV8jjUdyot\r
+ZE1XKjCwcCI9svX+VcVy/IxzuiDiiCHrBKZCGnXj77b8t3qQHBOm5o1KLFV+Dhdp\r
+qgghxAMMDqLICI8ngv9SflpzrrZ3TFnXuVESt0kzXzpnGoUc/7lZ0vMjPr/RBZBT\r
+zZ5CPXzYzywg6iY2UeifvQzQHX6H7TwEXMmb/XcJ0GcE57mZ/kwB7QxzCMrxrqgC\r
+/AaFnhFFNRDgVKoCOXCX9QR8uSKJ2CbQ9gPKKWSKy7RRi8Tj/kyPHLgi0ko9/Cwj\r
+jMfwH+6dU542GH8u6Plg4NpCPGMH+U/ldPpJ9oUnJZnNb6M604BIdgu9zNa9s+zm\r
+x2xg/qjq6XaaHjY2EnXamncDqzO8sXoSDx0SccJe8hyugliJ9IfohUtEo4buq3Kq\r
+ZuX9K0TBYEnEq6d4Laor\r
+=e0K3\r
+-----END PGP SIGNATURE-----\r
+--=-=-=--\r