I run a custom web-based email site. Some users requested the ability to smarthost any outbound mail from their accounts through an arbitrary server. In the past, we had accomplished this by running a separate virtual SMTP server for them, having the virtual server smarthost everything to that address, and passing the message off to that.

We started to look for an approach that would offer more granual control. Also, we wanted to get rid of the maintinance involved with setting up and running an bunch of virtual servers. Our idea was to insert a custom header item into the message, such as:

X-Smarthost: relay.server.com

... and to have the outbound mail server look for that setting and relay to the requested server.

Microsoft SMTP was a non-starter for this project. Sendmail was a dead-end. But I found a solution with Postfix. It's actually relatively simple, and despite some comments on usenet, was not at all a "hack", IMO.

First, enable header checks in /etc/postfix/main.cf:

>header_checks = regexp:/etc/postfix/header_checks

Then, insert a new header check in /etc/postfix/header_checks:

/^X-Smarthost: (.*)$/ FILTER smtp:$1

This will use a regex to match any email with a "X-Smarthost:" token in the header, and save the substring in the parenthesis to $1, which you can conveniently pass to the smtp transport. Any email not matching this rule will go through the default transport, which in a clean install means regular old DNS + smtp.

We tested the case where this smtp server is down. Mail was queued until it came back up, and delivered to the appropriate smarthost. We also tried setting the smarthost to a round-robin DNS A record, which also worked.