RE: Reply all - issue
[notmuch-archives.git] / 3d / 10ffb433cc4138b64333c5dc0588604a156b17
1 Return-Path: <jrollins@finestructure.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 3CBF1431FB6\r
6         for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 10:15:04 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.29\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.29 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3, T_MIME_NO_TEXT=0.01] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id LiBqWFTuEGgn for <notmuch@notmuchmail.org>;\r
16         Mon, 25 Jun 2012 10:15:02 -0700 (PDT)\r
17 Received: from outgoing-mail.its.caltech.edu (outgoing-mail.its.caltech.edu\r
18         [131.215.239.19])\r
19         by olra.theworths.org (Postfix) with ESMTP id E8C61431FAF\r
20         for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 10:15:01 -0700 (PDT)\r
21 Received: from fire-doxen.imss.caltech.edu (localhost [127.0.0.1])\r
22         by fire-doxen-postvirus (Postfix) with ESMTP id 551BE2E50E3C;\r
23         Mon, 25 Jun 2012 10:14:59 -0700 (PDT)\r
24 X-Spam-Scanned: at Caltech-IMSS on fire-doxen by amavisd-new\r
25 Received: from finestructure.net (m8.ligo.caltech.edu [131.215.115.108])\r
26         (Authenticated sender: jrollins)\r
27         by fire-doxen-submit (Postfix) with ESMTP id 135972E50E7B;\r
28         Mon, 25 Jun 2012 10:14:56 -0700 (PDT)\r
29 Received: by finestructure.net (Postfix, from userid 1000)\r
30         id AFC976DA; Mon, 25 Jun 2012 10:14:55 -0700 (PDT)\r
31 From: Jameson Graef Rollins <jrollins@finestructure.net>\r
32 To: David Belohrad <david@belohrad.ch>, notmuch@notmuchmail.org\r
33 Subject: Re: extract attachments from multiple mails\r
34 In-Reply-To: <uaxwr2vzkdh.fsf@beesknees.cern.ch>\r
35 References: <uaxwr2vzkdh.fsf@beesknees.cern.ch>\r
36 User-Agent: Notmuch/0.13.2+54~ga0426dc (http://notmuchmail.org) Emacs/23.4.1\r
37         (x86_64-pc-linux-gnu)\r
38 Date: Mon, 25 Jun 2012 10:14:53 -0700\r
39 Message-ID: <87obo72u0i.fsf@servo.finestructure.net>\r
40 MIME-Version: 1.0\r
41 Content-Type: multipart/signed; boundary="=-=-=";\r
42         micalg=pgp-sha256; protocol="application/pgp-signature"\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Mon, 25 Jun 2012 17:15:04 -0000\r
56 \r
57 --=-=-=\r
58 \r
59 On Mon, Jun 25 2012, David Belohrad <david@belohrad.ch> wrote:\r
60 > someone can give an advice? I have many emails containing\r
61 > attachment. This is typically an output of copy-machine, which fragments\r
62 > a scan into multiple attachments.\r
63 >\r
64 > I'd like to extract those attached files in a one batch into a specific\r
65 > directory. Is there any way how to programmatically fetch those files?\r
66 \r
67 notmuch show has a --part option for outputting a single part from a\r
68 MIME message.  Unfortunately there's currently no clean way to determine\r
69 the number of parts in a message.  But sort of hackily, you could do\r
70 something like:\r
71 \r
72 for id in $(notmuch search --output=messages tag:files-to-extract); do\r
73     for part in $(seq 1 10); do\r
74         notmuch show --part=$part  --format=raw $id > $id.$part\r
75     done\r
76 done\r
77 \r
78 That will also save any multipart parts, which aren't really that\r
79 useful, so you'll have to sort through them.\r
80 \r
81 You can make something much cleaner with python, using the notmuch and\r
82 email python bindings:\r
83 \r
84 http://packages.python.org/notmuch/\r
85 http://docs.python.org/library/email-examples.html\r
86 \r
87 I hacked up something simple below that will extract parts from messages\r
88 matching a search term into the current directory (tested).\r
89 \r
90 hth.\r
91 \r
92 jamie.\r
93 \r
94 \r
95 #!/usr/bin/env python\r
96 \r
97 import subprocess\r
98 import sys\r
99 import os\r
100 import notmuch\r
101 import email\r
102 import errno\r
103 import mimetypes\r
104 \r
105 dbpath = subprocess.check_output(['notmuch', 'config', 'get', 'database.path']).strip()\r
106 db = notmuch.Database(dbpath)\r
107 query = notmuch.Query(db, sys.argv[1])\r
108 for msg in query.search_messages():\r
109     with open(msg.get_filename(), 'r') as f:\r
110         msg = email.message_from_file(f)\r
111     counter = 1\r
112     for part in msg.walk():\r
113         if part.get_content_maintype() == 'multipart': continue\r
114         filename = part.get_filename()\r
115         if not filename:\r
116             ext = mimetypes.guess_extension(part.get_content_type())\r
117         if not ext:\r
118             ext = '.bin'\r
119         filename = 'part-%03d%s' % (counter, ext)\r
120         counter += 1\r
121         print filename\r
122         with open(filename, 'wb') as f:\r
123             f.write(part.get_payload(decode=True))\r
124 \r
125 --=-=-=\r
126 Content-Type: application/pgp-signature\r
127 \r
128 -----BEGIN PGP SIGNATURE-----\r
129 Version: GnuPG v1.4.12 (GNU/Linux)\r
130 \r
131 iQIcBAEBCAAGBQJP6JyNAAoJEO00zqvie6q8CmkP/0mZo91WaNASXNBL6+0OcvCT\r
132 eH9zyTd+veG6ctnGrFBqNyDlnijxCkSfIM8duQG/IrCd/MyUvb2rdQLa3SIxshaK\r
133 EqXVud8QyUyAH2e6fFR4NVsdLEEvJKOehwvYA6ZszUPTREprhuVKYxQUu1yjz6P5\r
134 0n91Wwn51nV5mCG154qSr3zR1ODF2ah97eol3ZWH0YccVlRpc3rs1nhpFDFkIZQS\r
135 4ivK4cnf47Ffp68up7Uo3msQcj4D0kXvtMG8cr2Bq5ifzq57nQuh72EV8jjUdyot\r
136 ZE1XKjCwcCI9svX+VcVy/IxzuiDiiCHrBKZCGnXj77b8t3qQHBOm5o1KLFV+Dhdp\r
137 qgghxAMMDqLICI8ngv9SflpzrrZ3TFnXuVESt0kzXzpnGoUc/7lZ0vMjPr/RBZBT\r
138 zZ5CPXzYzywg6iY2UeifvQzQHX6H7TwEXMmb/XcJ0GcE57mZ/kwB7QxzCMrxrqgC\r
139 /AaFnhFFNRDgVKoCOXCX9QR8uSKJ2CbQ9gPKKWSKy7RRi8Tj/kyPHLgi0ko9/Cwj\r
140 jMfwH+6dU542GH8u6Plg4NpCPGMH+U/ldPpJ9oUnJZnNb6M604BIdgu9zNa9s+zm\r
141 x2xg/qjq6XaaHjY2EnXamncDqzO8sXoSDx0SccJe8hyugliJ9IfohUtEo4buq3Kq\r
142 ZuX9K0TBYEnEq6d4Laor\r
143 =e0K3\r
144 -----END PGP SIGNATURE-----\r
145 --=-=-=--\r