[SPAM?] Re: [SPAM?] Re: [SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-15 Thread Luis Mochan
On Thu, Sep 15, 2016 at 11:33:41AM +0200, Richard Z wrote:
> On Thu, Sep 15, 2016 at 10:53:02AM +1000, Cameron Simpson wrote:
> > On 14Sep2016 16:12, Are Troi  wrote:
> > > Last night at a technical talk I lamented the loss around 5 years ago
> ...
> tried ripmime and munpak about a year ago to save all attachments from 
> a large MBOX and it was everything but a hasslefree experience. Various 
> attachments were missed or not processed, some segfaults.
> Those tools are apparently not as robust as mutt is.

I have used ripmime without problems but on individual messages from a
Maildir, not on a large mbox.

Luis




Re: [SPAM?] Re: [SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-15 Thread Richard Z
On Thu, Sep 15, 2016 at 10:53:02AM +1000, Cameron Simpson wrote:
> On 14Sep2016 16:12, Are Troi  wrote:
> >Last night at a technical talk I lamented the loss around 5 years ago
> >from Fedora of command-line tools to extract email attachments from a
> >BASH script and a colleague told me Mutt can do this.
> 
> If you mean the MIME tools mpack and munpak, I still use them. (On a Mac,
> where the Macports package is called mpack).
> 
> Wouldn't you be better off just fetching and buiulding them?

tried ripmime and munpak about a year ago to save all attachments from 
a large MBOX and it was everything but a hasslefree experience. Various 
attachments were missed or not processed, some segfaults.
Those tools are apparently not as robust as mutt is.

Ended up splitting the MBOX by formail 
  cat MBOX | formail -ds sh -c 'cat > msg.$FILENO'
and processing messages by 
  ripmime -v  --name-by-type --verbose-contenttype --verbose-defects  -i "$f"
.. and hoping not much was missed by this apporach.

Richard

-- 
Name and OpenPGP keys available from pgp key servers



[SPAM?] Re: [SPAM?] Re: [SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread David Champion
* On 14 Sep 2016, Cameron Simpson wrote: 
> 
> Mutt is probably a poor match for the task because although it will decode
> messages etc, all the saving is interactive. In particular, there's no API
> for "iterating" over attachments, let along recursively.

Agree. It's entirely doable, but not worth the trouble and the
maintenance when there are other fine options.

> I'd be going for the Python stuff, lacking your context.

See attached.

You can pipe a message into this program (within mutt or elsewhere):

| mutt-savefiles /tmp/foo

It will create a directory under /tmp/foo named for the message's
message-id, and store each attachment inside. Filenames are taken
from the MIME or generated sequentially if there is no filename.

-- 
David Champion • d...@bikeshed.us
#!/usr/bin/env python
#
# TODO: merge into sympafile
#

import os
import sys
import email
import mimetypes

m = email.message_from_file(sys.stdin)

# mimetypes very unfortunately maps text/plain to .ksh, so
# we'll favor this internal list in type lookups.
localtypes = {
'text/plain': '.txt',
}

if 'message-id' in m:
msgid = m['message-id'].strip('<>')
else:
msgid = str(time.time()).replace('.', '_')

if len(sys.argv) > 1:
dirname = sys.argv[1]
else:
dirname = '.'

dirname = os.path.join(dirname, msgid)
try:
os.makedirs(dirname)
except:
pass

n = 0
for p in m.walk():
n += 1
mtype = p.get_content_type()
if mtype.startswith('multipart/'):
# container
continue

ext = localtypes.get(mtype.lower()) or \
  mimetypes.guess_extension(mtype) or \
  '.bin'
filename = p.get_filename() or ('%02d%s' % (n, ext))
filename = os.path.join(dirname, filename)
data = p.get_payload()
print filename, len(data)
fp = open(filename, 'w')
fp.write(data)
fp.close()


signature.asc
Description: PGP signature