34

I'm using postfix for my email. I know I can use /etc/aliases and newaliases command to redirect certain email, eg I can redirect root@example.com to user@example.com by putting root: user in my /etc/aliases and then running newaliases. However I'd like to do the equivilant of *: user, so that all email to example.com will get sent to user@example.com.

How do I do this?

Amandasaurus
  • 31,471
  • 65
  • 192
  • 253

7 Answers7

21

luser_relay = user@example.com in your main.cf is what you want.

To disable user checking and accept all mails you need to add the local_recipient_maps = option. Documentation

chicks
  • 3,793
  • 10
  • 27
  • 36
womble
  • 96,255
  • 29
  • 175
  • 230
  • 1
    I added that to main.cf and restarted postfix. I got the following error in /var/log/main.info:
    550 5.1.1 : Recipient address rejected: User unknown in local recipient table; from= to=
    – Amandasaurus Jun 10 '09 at 23:57
  • So example.org isn't in your local recipient domains list, or something else has been misconfigured. – womble Jun 11 '09 at 00:21
  • 7
    I got it working. Aswell as the luser_relay option you mentioned you also need to add "local_recipient_maps =" option. This postfix documention page explains that: http://www.postfix.org/postconf.5.html#luser_relay – Amandasaurus Jun 11 '09 at 13:50
  • Does this method preserve the To email address? (the virtual maps rewrites it and the original To: address is lost.) – user14645 Jul 06 '15 at 19:51
  • @user14645 envelope or mail headers To address? – womble Jul 07 '15 at 01:46
  • that works fine for a single recipient, but how do we deal with this if there are several recipient, and we only want to allow one of them to forward, but not the other (they should be caught by the rule above). – Sverre Mar 31 '16 at 13:35
  • 1
    @Sverre questions are for questions, not comments on seven-year-old answers to tangentially-related questions. – womble Apr 04 '16 at 02:14
  • This doesn't seem to answer what the user requested, despite his acceptance. For some weird reason, the `local_recipient_maps = ` does NOT shut off delivery to local accounts that exist. Thus, relaying only works for accounts that don't already exist. – Otheus Dec 05 '20 at 02:59
18

What you are looking for is a virtual alias table and can be done using the following...

First you need to edit, or create if it doesn't already exist, the /etc/postfix/virtual file.

example.com   whatever
@example.com  user@example.com

After this has been saved you will need to run postmap /etc/postfix/virtual in order to generate the indexed /etc/postfix/virtual.db Postfix will read.

You will then need to edit the /etc/postfix/main.cf and be sure that you have the following line uncommented:

virtual_alias_maps = hash:/etc/postfix/virtual

This will actually let postfix know about the virtual alias table and use it.

The problem with using the luser_relay option as others have mentioned is that this is treated as the user of last resorts for unknown addresses by the local delivery agent. If postfix is only handling mail for one domain this can be utilized for this but it can mask other configuration errors and will likely cause unexpected results if more than one domain is being handled.

The one caveat I feel obligated to mention is that by creating a wildcard alias of this nature you are opening up your mail system to accept messages for addresses that will not exist and run the risk of having the mailbox filled quickly with spam for non-existent mailboxes.

Jeremy Bouse
  • 11,341
  • 2
  • 28
  • 40
9

It's actually quite simple. All you need to do is to put something like this in your main.cf file:

virtual_alias_maps = regexp:/etc/postfix/virtual_alias

The regexp part does the trick. Also, in /etc/postfix/virtual_alias you put something like this:

/^test/ you@yourdomain.com

In this example you should receive all mail with destination starting with 'test'. Don't forget to postmap /etc/postfix/virtual_alias and reload postfix.

Andrew Schulman
  • 8,811
  • 21
  • 32
  • 47
patryk
  • 91
  • 1
  • 1
8

To combine an answer/comment above that worked easiest for me (I have one primary domain):
Add the following to /etc/postfix/main.cf :

luser_relay = user@domain.com (or local system user)
local_recipient_maps =

(local_recipient_maps = has no value set)

I use a local user linux account and also add same user to /etc/aliases so that user gets all mail to root,postmaster,etc.

Note: luser_relay works only for the Postfix local(8) delivery agent.

For virtuals, you can use virtual_alias_maps and point any virtual domain addresses/catchalls at your main email account.

Please consult luser_relay or virtual_alias_maps for more info.

B. Shea
  • 1,009
  • 11
  • 22
5

If you want to use a catch-all address for mydomain, you can use /etc/aliases combined with a regex as follows:

In /etc/postfix/main.cf add
alias_maps = hash:/etc/aliases, regexp:/etc/postfix/catch-all-local.regexp

In /etc/postfix/catch-all-local.regexp add
!/^owner-/ your-catch-all-user

This should do the trick. It worked for me, and works with virtual_alias_maps set in parallel.

Vasile Goian
  • 166
  • 1
  • 3
3

There is a less known static lookup table type in Postfix, which can be used for exactly this purpose:

alias_database = static:user@example.com
alias_maps = static:user@example.com

If user@example.com is not local for this system, you'd also probably want to discard all the bounce messages by replacing lines ending with bounce with discard in master.cf to avoid the mail loops.

-4

I hate to be a killjoy, but your problem is that you're using Postfix. Postfix makes precisely this exercise very difficult. I know: I had to do it some years ago and it took days and days to get it right. The luser option is the right one, but there are other things to get right, too. (And even the name of the option shows that Postfix's authors didn't really want to support this.)

I recommend switching to Courier. Courier supports this "catch-all" method in a much more sensible way because of the way it extends its existing alias system.

staticsan
  • 1,529
  • 1
  • 11
  • 14
  • 1
    Postfix doesn't make it difficult at all. Virtual aliases are the standard way to do catchalls in Postfix and they're very easy. That said, on anything but a toy mail server for your own personal domain, catchalls are a very bad idea. – Rob Chanter Jul 26 '09 at 23:38