Sunday, May 10, 2015

Strip attachments and save to directory

Did you ever wondered how to store mail attachments outside of mailboxes?

Below you'll find step-by-step instruction how to do it with MSH Traffic Policy application. This instruction will work with Postfix, Zimbra and Sendmail mail server.

Run MSH Traffic Policy Administrator desktop application (Pic. 1).

Pic. 1

Before creating a new rule you need to prepare a template which will be added to each message. This template will be inserted on top of the message, but only when attachments were removed.


Select Templates node and click Add button (Pic. 2).

Pic. 2

In the new opened window put template name ie. Remove Attachments Template.

Create template for both types of messages, Html and PlainText. Template should contain information which attachments were removed and where they are saved. Application can automatically insert attachments file names into template, you need to use advanced variables.

For example if you would like to add into message information like this (red rectangle) (Pic. 3):

Pic. 3

You need to create the template like this (Pic. 4):

Pic. 4

Where most important part is message attachments advanced variable:


Syntax of this variable is:


Where <PREFIX> is some text which will be inserted before each attachment file name. In mine example prefix is:

  • <NETWORK_SHARE>/mail/attachments - it's a location were you'll be storing attachments. It could be a network disk, cgi url, ftp or anything which will bring users to attachments located on the server.
  • ${message:id} - will be resolved to unique message identifier

Thanks to this only recipients of stripped message will know where are attachments located.

Don't forget to define PlainText template in the same way as for Html. Click OK button to accept the template.

Rule Actions

Now it's a time for creating a rule which will remove attachments from messages and save them to the specified location on the server.

Click Add Group button to create new group, call it Strip Attachments. Expand Rules node and select Strip Attachments group.

Click Add rule button. On Actions page click Add button and select Attachments > Strip Attachments action (Pic. 5).

Pic. 5

Provide absolute path to directory were attachments will be saved. Remember that this location is on the mail server and attachments from each message should be in unique directory, use ${message:id} advanced variable. Select Remove Attachments template and click OK button (Pic. 6).

Pic. 6

Rule Conditions

Now you need to define which messages should get stripped. You could set rule for internal or incoming message, it's up to you. In this example I'll made conditions for internal messages.

On Conditions tab click Add button and select Message Sender - Email Address condition. Select Wildcards option and put * value into Email addresses field. Click OK button (Pic. 7).

Pic. 7

Click again Add button and select Message Recipients - Email address menu. Select in TO, CC and BCC field option to search address in all message address fields. Select Wildcards option, put * value into Email addresses field.

Now select True only if all recipients are matched option, this is important because when one of the email addresses will be an external recipient stripping of attachments should be skipped. If not external recipient will have no access to attachments stored in network share. If you will implement solution when users will access attachments by http server which will be available from internet then you should select True only if at least one recipient is matched option. Click OK button (Pic. 8).

Pic. 8

Make sure that All conditions met option is selected, so rule will be applied only when sender and recipients are matched (Pic. 9).

Pic. 9

On the Name tab set Strip Attachments name of the rule.

Click Save All button to store settings.


Now send some test e-mails to your fellow in the company and check if everything is working fine.

So if you need to strip, remove, detach or dump attachments and insert information to the message use MSH Traffic Policy. Don't forget that this is only an example of one functionality, to learn more go to the application website.