Route mail in postfix to different relays based on subject

I am looking to configure postfix to route messages to different relays based on the contents of a subject line.

The desired result would be if a user sent an outbound e-mail with a specific tag say "SECURE:" at the start of the subject line it would forward to one relay (1.1.1.1) but if the tag was not specified the message would be delivered another relay (2.2.2.2).

I have investigated header checks, before and after message filtering, and postfix proxy but I have not been able to pin down how to accomplish changing the mail route based on message contents (which seems like it should be a fairly simple thing to do).

I might expect to involve procmail for this task, but would like some assistance in where to begin. Can anyone assist?


If I understand the manual correctly, you can achieve this using header checks with FILTER transport:destination.

From the header_checks part of the manual:

The transport name specifies the first field of a mail delivery agent definition
in master.cf; the syntax of the next-hop destination is described in the manual page
of the corresponding delivery agent.

In your case, this would be smtp:1.1.1.1

So the full config:

In main.cf:

header_checks = regexp:/etc/postfix/header_checks
relayhost = 2.2.2.2

In header_checks:

/^Subject: SECURE:/ FILTER smtp:1.1.1.1

I haven't tested this at all.