Remove/Delete Attachments from email server (IMAP)

emailimap

I have a rather large quantity of emails on my mail server (IMAP) which contain attachments (PDF's, etc.) These attachments waste space–my HDD space on server is limited by my hosting company–and I am trying to regain some of that space by removing the attachments from the emails. (These are redundant on the mail server as they have pretty much all been downloaded or uploaded locally.)

When I used Windows (a long time ago) there was an app called "IMAPSize" which was a "client-type" email handler which could be used to login to mail server and manipulate the messages, remove attachments, etc. But I have not found (so far) anything comparable in terms of ease of use and getting the job done, for Unix/Linux (although we might have much better and more robust solutions–I just don't know which.)

I am aware of munpack (have installed and used it locally) and ripmime but it seems to me that those programs are meant to handle messages when they are stored locally, Mbox or MailDir. I do not sync my email messages locally, preferring to leave them on our server (have so many emails, over 100k, in aggregate.)

Moreover, Munpack seems to be great at extracting attachments but I have not seen a way to just delete the attachments, without destroying the message itself.

I do have Horde, Roundcube and SquirrelMail available as web GUI's but, again, have not seen a way to delete only the attachments.

Additionally, I am looking for a solution which does not involve complex setup and configuration, etc. (I may be dreaming/asking for too much.)

I thought of trying this approach: use offlineimap to download and sync all of my emails locally, then possibly use munpack or ripmime to delete attachments (provided I can find a way to do that, as opposed to only extracting them) and then re-sync with server and hopefully, emails would be back on server without attachments but a) not sure if this approach would work and 2) again, I am not really looking to download tens of thousands of emails, locally….

I am sure there is a solution out there for me (perhaps some type of client-type GUI app which would let me manipulate emails on servers/delete attachments; or some other not too complex approach.)

Note: I do not use Thunderbird and have no desire to install or use it; and same for Wine. I try to keep my box very 'minimal' with light footprint apps, wherever possible.

Thanks for a pointer in the right direction.

Best Answer

I had the following bit of perl that strips all attachments from stdin and returns stdout which may be of help:

#!/usr/bin/perl -w
use strict; 

use Mail::Audit; 
use Mail::Audit::Attach qw(Attach); 

my $mail = Mail::Audit->new; 

my $attachments = $mail->attachments; 

foreach (@$attachments) 
{ 
      $_->remove; 
} 
$mail->print();

and than a simple loop over the files in your Maildir you want stripped of attachments for isntance:

for filename in <list> 
do 
  ./strip.pl < "$filename" > "$filename".lock && mv "$filename".lock "$filename" 
  rm "$filename".lock
done 

An interesting modification could be to first extract the attachments and store them separately first before deleting them from the mails:

foreach (@$attachments) 
{ 
      $_->save("/path/to/attachment/dir");
      $_->remove; 
}

Make a back-up first though ;)

Related Question