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.


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) 

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

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

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

foreach (@$attachments) 

Make a back-up first though ;)

|improve this answer|||||
  • Thanks for feedback. That assumes I am downloading/syncing my emails from IMAP to local which I'm trying to avoid. But there might be no way around it; don't know yet. – stef204 Dec 17 '14 at 17:53
  • This runs on the server where the mail messages are stored, not necessarily locally, but assumes you have shell access there. I tested om my own mailserver with a smallish IMAP folder with a couple of hundred messages. – HBruijn Dec 17 '14 at 17:58
  • @stef204: hadn't read your original question carefully enough, just recognised it as something I had done before, I thought... – HBruijn Dec 17 '14 at 18:05
  • I'm not sure I can have shell access to the server (I guess you mean via ssh); it's a on a shared hosted server. I do have admin rights so might be possible. I'll look into it. And let's see if can I can get additional feedback here as well. Tx. – stef204 Dec 17 '14 at 18:32

You could use Thunderbird and the AttachmentExractor Addon for this. Simply add your mail account to Thunderbird and then start the AttachmentExtractor. It can remove the content of attachments while keeping the attachments filename in your mail.

|improve this answer|||||

Removing the attachments on the server is quite easy, they are only (text) files. - Allocate mails, having an attachment (usually the larger ones):

find -P /home/*/mail/*/*/* -mindepth 1 -maxdepth 1 -name "*=*" -size +2M -mtime '+367'

It works 100% well on WHM server with mail directory, not tested with mbox

  • Loop over the files
  • if grep -e '------=_' ${file}; # containing strings .... then
  • split the emails on common criteria, eg.: csplit --prefix=split4mail2clean. -z ${file} /commonstring/
  • allocate/find within the split files you strings, attachments will have some typical strings, delete those parts
  • join the temporary split files left (those without the typical attachments) in reverse order to rebuild the original email
  • reapply the modification timestamp
  • do other things here, if needed

In order to succeed, you might need 3, 4 possible more IF conditions and delete if file contains this string ... Run this shell script recommended from a temporary folder, adjust sizes and other search criteria. Yes, I have a working script, tested on some 40000 mail files and it's leaving 0 attachments, within the specified search and delete criteria.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.