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