Monday, October 28, 2013

Filter all recipients with Postfix and Sendmail



I've heard several times that someone wants to block message, save attachments or add disclaimer only to one type of emails. To messages which are from/to specific group of people.

Example: save attachments from all messages but skip messages from one particular domain, so if email is sent only to @gmail.com recipients then don't save, otherwise save.

from: someone@dot.com
to  : john@gmail.com
DON'T SAVE

from: someone@dot.com
to  : john@gmail.com
cc  : mike@gmail.com
DON'T SAVE

from: someone@dot.com
to  : john@gmail.com, jane@outlook.com
SAVE!

from: someone@dot.com
to  : jane@outlook.com
cc  : mike@outlook.com
SAVE!

HOW TO DO IT? Use MSH Traffic Policy!


First we need to define new Save Attachments action. It needs absolute path to directory where files will be saved. Path supports advanced variables, so it's possible to build hierarchy of directories based on message properties. Attachments have variety of file names, for example "...dont wait.jpeg", if file will be directly saved to disk, on some system may not be available or will be hidden (first dot). Select "Replace first dot with underscore in attachment filename" option to prevent this, now attachment will be saved as "_..dont wait.jpeg".

Save Attachments action
Now it's time do make some conditions and exceptions for our case. First we need to define condition which will match all messages, we can use Message Sender condition with asterisk as a value.

Exceptions should skip messages which are addressed only to @gmail.com recipients. If at least one recipient is not @gmail.com then attachments should be saved. Select Message Recipient exception, it will search for *@gmail.com addresses using wildcards. Also regular expressions (regex) can be used to match more sophisticated pattern. Recipients must be searched in all fields TO, CC, BCC. Our exception must be true only if all recipients are matched, otherwise it should fail.

Recipient email address pattern and searching options

SUMMARY


This is only one example of condition/exception matching message recipients. MSH Traffic Policy application offers many variety options to check/parse/match any message value (like size, type or attachments count) and with much more actions so that your work with email servers will be more fun.

Maybe you're looking for something different: