5

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.

4 Answers 4

3

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 ;)

4
  • 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
    Commented Dec 17, 2014 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
    Commented Dec 17, 2014 at 17:58
  • @stef204: hadn't read your original question carefully enough, just recognised it as something I had done before, I thought...
    – HBruijn
    Commented Dec 17, 2014 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
    Commented Dec 17, 2014 at 18:32
1

I tried IMAPSize as well, but it is no longer supported, crashed sometimes, and left behind messages that it seems it should have deleted. There is also the Unattach app for GMail users, though I can't vouch for it.

To solve it for myself, I made a python script that may be of use: IMAP Size Reducer

It works on one specified folder per run. As long as you know how to run python scripts on the command line, how to log in to your server (URL, username, password), and the IMAP folder specification (e.g. "INBOX.Sent"), it should work with any IMAP folder. (Your ISP can help with those details if necessary.)

You can specify the "max desired message size" (i.e. "if the message is larger than this size, remove attachments until it is under this size") and "tolerable part size" (i.e. "if an individual message part is under this size, don't delete it, no matter the overall message size").

The following MIME types are considered candidates for part deletion:

Anything with Content-Disposition "attachment"
Content-Type image/*
Content-Type audio/*
Content-Type video/*
Content-Type music/*
Content-Type x-music/*
Content-Type application/*

It has not been rigorously tested on various email providers, but it worked well for me. See the web page for usage details.

6
  • Whilst your page that you link to seems useful, this really is a link only answer and it would be much better to post both the script and the relevant contents of the page in your answer. Please edit and update your answer. Commented Mar 30, 2022 at 22:43
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review Commented Mar 30, 2022 at 22:43
  • Thanks, I wasn't aware that this was frowned upon. Is the situation any different given that the question was explicitly asking for a solution, such as the one I linked to, as opposed to asking for information or details about how it operates? In other words, what kind of "essential parts of the answer" should I include here, since they weren't asking for technical details on how to free space in IMAP folders? Or is it just standard protocol to include some details about what the link is, how it operates, etc?
    – user272901
    Commented Mar 31, 2022 at 1:56
  • This seems fine to me — it doesn’t make sense to include the script in the answer, and there’s some explanation of what it does. Commented Mar 31, 2022 at 4:11
  • Thanks StephenKitt. @Greenonline -- I added some explanatory information -- is that more what you had in mind?
    – user272901
    Commented Mar 31, 2022 at 16:03
0

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.

-1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .