-   Linux - Server (
-   -   Postfix "Content Filter" setup to reject emails with too many recipients in TO: field (

bahadirtonguc 11-02-2008 02:17 PM

Postfix "Content Filter" setup to reject emails with too many recipients in TO: field
Dear all,

I am willing to setup a content filter to reject messages with too many recipients in TO: or CC: fiels but to let for BCC:

This way suppose I may avoid mistakes such as putting all the recipients in TO: or CC: fields instead of BCC: so this way they wont ever see each other.

(Some of my colleagues had done it quite a few times in the past and exposed all our private address book to every recipient.)

Appreciate your help


bahadirtonguc 11-16-2008 05:08 PM

It's quite strange that nobody could reply however I helped myself reading a lot about this question and would like to share with others:

in header_checks, write a regexp like below which regret not working and needs a bit modification


Don't know where I am doing wrong but feels like very close to the answer though :)

billymayday 11-16-2008 05:23 PM

You probably need to work in a delimiter between emails as well

Berhanie 11-16-2008 06:57 PM

Here's a pcre regexp that matches at least 5 '@' in the To header:

^To: ([^@]*@){5,}
Note that if you reject such messages, you will also be filtering incoming messages
from the outside, so for example you would reject mail addressed to you and four other recipients.

bahadirtonguc 11-17-2008 04:41 AM

It aint working mate. Still can successfully send any kind of messages outbound. By the way, I dont use this server for inbound traffic so not concerned about filtering incoming messages.

Berhanie 11-17-2008 07:50 AM

In that case, you should post your header_checks file, so people can see what's not working. Your file should look something like this:

/^To: ([^@]*@){5,}.*/      ACTION
I tested the expression in my first post with pcretest. For postfix, I think you
need to match the entire line (i.e. header), which is why you need the ".*" at the end. I haven't tested it with postfix, so you might have
to improve it.

bahadirtonguc 11-17-2008 09:41 AM

Here is the solution :

/^To: (.*@.*){5,}/ REJECT too many recipients
/^Cc: (.*@.*){5,}/ REJECT too many recipients

Berhanie 11-17-2008 10:32 AM

You're welcome.

bahadirtonguc 11-17-2008 03:26 PM

Berhanine, thanks for your help but regret it did not work out. So I posted the correct syntax

Berhanie 11-17-2008 04:40 PM


but regret it did not work out
Hi, bahadirtonguc. In the course of this thread, you went from writing a nonsensical regular expression at the beginning to solving your own problem by the end. So, I would say that it did work out. Good job!

akelder 05-27-2010 02:07 AM

worked for me
I realize this is an old post, but just wanted to report what worked for me, in case someone else is looking to do this.

Berhanie's solution worked, here's my config.



# restrict based on message header content
header_checks = pcre:/etc/postfix/header_checks



/^To:([^@]*@){10,}/        REJECT Sorry, your message has too many recepients.
/^Cc:([^@]*@){10,}/        REJECT Sorry, your message has too many recepients.

On the other hand, bahadirtonguc regex caused Postfix to choke:


Out: 220 ESMTP Postfix (No UCE)
Out: 250
Out: 250 2.1.0 Ok
In: RCPT TO:<>
Out: 250 2.1.5 Ok
Out: 354 End data with <CR><LF>.<CR><LF>
Out: 451 4.3.0 Error: queue file write error
Out: 221 2.0.0 Bye


May 26 20:17:00 postfix/cleanup[14318]: fatal: pcre map /etc/postfix/header_checks, line 2: matched text exceeds buffer limit
Reject an email message with too many recipients with Postfix

All times are GMT -5. The time now is 10:15 AM.