How to use metamail to extract mail attachments without interactive input?

I am looking for a way to save all attachments from an IMAP folder with 3000+ DMARC reports each of which is attached inside the individual messages as .zip or .xml.gz file. I am using Claws Mail which has a way to automate this (See Examples -> Save) through munpack. Unfortunately on openSUSE Leap 42.3 (which I use) there doesn't seem to be a package which provides munpack. After some web searching I found an alternative: metamail -wy suggested as an answer to this question.

Trying that directly in claws gave me some error (as not using properly the program). So I saved manually one message in a temp directory and tried from console:

[/tmp/download]: metamail -yw msg
Date: Mon, 03 Nov 2014 10:13:53 +0000
Subject: Report domain: ████████ Submitter: ████████
From: [email protected]
To: ████████


This message contains data in an unrecognized format, application/zip,
which can either be viewed as text or written to a file.

What do you want to do with the application/zip data?
1 -- See it as text
2 -- Write it to a file
3 -- Just skip it

2
Please enter the name of a file to which the data should be written
(Default: ████████.zip) >
Wrote file ████████.zip

In other words: It works per se but it requires interactive input for each individual message - surely not an option for 3000+ mails.

My question is:

How can I use metamail in a way which doesn't require interactive input, so that it can be iterated (e.g. in a script or through Claws Mail's functinality)?


Solution 1:

I'm not sure how the data you have actually looks like, so the best I can do is some educated guesses.

I used metamail -q -w -x file_name successfully in the past in scripts to extract attachments. As you can see from man metamail, -w tells metamail not to consult the mailcap file, but just extract the attachments, -q tells it to be quiet, because scripts don't like the extra output, and -x tells it that it's definitely not running on a terminal, so it shouldn't ask stupid questions.

You already have -w, and you are also using -y, which shouldn't be necessary if the mail is properly formatted, but maybe yours isn't - as I said, I don't know what data you have. The man page also warns about strange effects that can happen with -y.

So first drop -y, see if it still works. Then add -x, see if it stops asking. Finally add -q to remove unwanted output.

Edit

After being accused to be too lazy to try, I did the following steps:

1) Mailed myself a zip file with one empty file using mutt. Here's the complete mail, with some headers removed/edited:

From mail  Sat Mar  3 12:49:13 2018
Envelope-to: <dirk@...>
...
Message-ID: <20180303114913.xr7xvpx2pso6wurn@feanor>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="gsrhzrdopn4ddt7t"
Content-Disposition: inline
User-Agent: NeoMutt/20170609 (1.8.3)
Content-Length: 480
Lines: 18


--gsrhzrdopn4ddt7t
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Test

--gsrhzrdopn4ddt7t
Content-Type: application/zip
Content-Disposition: attachment; filename="foo.zip"
Content-Transfer-Encoding: base64

UEsDBAoAAAAAALplY0wAAAAAAAAAAAAAAAAHABwAenp6L2Zvb1VUCQAD8IqaWvCKmlp1eAsA
AQToAwAABOgDAABQSwECHgMKAAAAAAC6ZWNMAAAAAAAAAAAAAAAABwAYAAAAAAAAAAAApIEA
AAAAenp6L2Zvb1VUBQAD8IqaWnV4CwABBOgDAAAE6AMAAFBLBQYAAAAAAQABAE0AAABBAAAA
AAA=

--gsrhzrdopn4ddt7t--

2) Saved it into a mailbox called yyy, then ran

METAMAIL_TMPDIR=. metamail -q -x -w ~/Mail/yyy

3) As expected, metamail doesn't ask, and saves all parts as two files called 1-M64bZlz and 1-MKfamEn. The second is the zip file, the first is the body text.

And now? It works for me, it doesn't work for you.