Filter for mail attachment size ? (to be used from Procmail or similar)

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have been looking for a long time for a filter on attachment size in

At the mailserver, which is running Postfix and Procmail, there
already is a an auto-reply filter in Perl that checks a DB for status,
so filtering with Perl in general works.

Here is what I would like to do:

* check mail if there is any attached files
* if attachment is above (xxx) bytes - this can be read from a
database per user - remove the attachment and send a reply back to
sender that they need to use an upload service.
* then forward rest of mail (main body) to recipient

I have studied the docs on Mail::Simple etc, but have not been able to
understand how.

So, anyone with a completed script or points on how to get started on
this ?

Ultimate goal is to get rid of people that mail 10Mb+ screenshots to
explain a simple thing.
PS: if anyone has a pointer on how to do this in Procmail or other
scripting languages, fine with me..


Re: Filter for mail attachment size ? (to be used from Procmail or similar)

Me three, four, five :-)-O

I am more interested from the perspective of doing really weird
stuff in developing countries, which involve cutting off of
attachments, batching and gzipping, and perhaps even uucp again :-)-O

Of course at the same time one would need to check for the message
size so that one catches uu/mime-encoded stuff in the text, which
doesn't gzip nice and as such could just be bounced altogether...


on 2011-12-20 11:38 Horst Lemminger said the following:
Quoted text here. Click to load it

Dr. Eberhard W. Lisse  \        / Obstetrician & Gynaecologist (Saar)
el@lisse.NA el108-ARIN / *     |   Telephone: +264 81 124 6733 (cell)
PO Box 8421             \     /   Please do NOT email to this address
Bachbrecht, Namibia     ;____/        if it is DNS related in ANY way

Re: Filter for mail attachment size ? (to be used from Procmail or similar)

Quoted text here. Click to load it

I would recommend setting Postfix's message_size_limit parameter
instead. This won't do exactly what you want: it will bounce any message
which is over the limit, regardless of whether the overrun is in an
attachment or not, and it will not attempt to remove the oversized
section and forward the rest. This may be unacceptable to you: the
advantage is it will be much more efficient.

If you are going to be auto-replying to emails yourself (rather than
letting Postfix do it), you need to *be* *very* *careful*. This sort of
application often ends up doing more harm than good. At the very least
you need to read and understand RFC 3834.

That said, there are (AFAIK) two basic options for extracting
attachments in Perl: Email::MIME and MIME::Parser. Email::MIME is much
simpler and faster, but requires that you hold the whole message (and
all the extracted parts) in memory. If you're potentially dealing with
very large messages, this might be a problem (certainly, you would need
to set Postfix's message_size_limit to something sensible in this case).

MIME::Parser will extract the attachments to temporary files if it needs
to, so it can deal with arbitrary-sized messages without a problem. The
disadvantage is that every time a mail delivery touches the disk it
slows things down horribly. (Of course, if you're already invoking a
fresh instance of perl from procmail for every delivery, you maybe don't
care about that.)

Quoted text here. Click to load it

Pick one of those modules, read the docs carefully, and see how far you
can get. Post what you end up with, and people might help you improve


Site Timeline