You can use the redirect router to process alias messages. This is also the one which runs scripts / forward files given by the users. The processing of the routers happens from top to bottom, so the example below works roughly like this:
- The
syswide_alias
router checks aliases in the /etc/aliases
file. If one is found, the routing sequence restarts.
- The forced_delivery makes the same check, but this time it uses the
original_local_part
variable which is empty if no forwarding were done. If the lookup is successful, the message is delivered. In this case the processing stops (since a delivery was done), so any filters or forwards the user might have are ignored.
- If the
forced_delivery
router declines, the .forward
file will be processed. If this file contains aliases, the routing sequence restarts.
- Finally, the message gets delivered to the user's mailbox. This will happen only if the filter file in the previous step didn't result in a significant delivery (that is, it doesn't contain a forward alias, or doesn't drop the message entirely).
The routers
section should look like this:
syswide_alias:
driver = redirect
allow_defer
allow_fail
data = ${lookup{$local_part}lsearch{/etc/aliases}}
forced_delivery:
driver = accept
allow_defer
transport = local_delivery
condition = ${if eq {${lookup{$original_local_part}lsearch{/etc/aliases}}}{}{no}{yes}}
user_filters:
driver = redirect
allow_defer
allow_fail
allow_filter
file = ${home}/.forward
normal_delivery:
driver = accept
check_local_user
transport = local_delivery
Both the syswide_alias
and the forced_delivery
routers have the allow_defer
option, which is needed if the file is not found (which is probably won't be the case, but it doesn't hurt anyway)
Edit: The above example now contains a full set of routers needed to achieve the goals of the question.