# [SOLVED]Postfix change alias/box order (catchall problem)

## Cyberwizzard

Hi,

I have a problem: one of our customers wants a catch-all as a backup option while still using normal mail boxes. Our setup is mostly like the one from the wiki.

As far as I can gather from the logs, the virtual alias map is searched for a hit which turns up nothing except for the catch-all which is then resolved in the (local) virtual mailbox. This effectively bypasses the mailbox map altogether for that domain.

I want Postfix to use the mailbox map before checking the alias map - this would give me the desired behaviour: existing addresses are expanded to a mailbox or direct forward address, when nothing is found, the last option in the alias map (which is generated by MySQL btw) is the catch-all which is then allowed to its thing.

I found others with the same issue but no solution. I also checked the Postfix docs but I couldn't find any option which does this.Last edited by Cyberwizzard on Mon Dec 17, 2007 9:16 pm; edited 1 time in total

----------

## elgato319

 *Quote:*   

> 
> 
> Our setup is mostly like the one from the wiki. 
> 
> 

 

Could you tell us which wiki you followed?

----------

## Cyberwizzard

It looks like the manual has been altered from the last time I needed it but its this one:

http://gentoo-wiki.com/HOWTO_Linux_Virtual_Hosting_Server#Virtual_Mail_Hosting

Postfix+MySQL+Amavis+ClamAV+SpamAssassin+Courier-IMAP+Courier-Authlib

----------

## kashani

You're in for a bit of pain, but this is easy to fix. 

Postfix always checks the alias table before moving on to the user table. This makes sense when you thing about it. Because your catchall is matched Postifx assumes its job is done and dumps all mail into the catchall without looking at the user table. 

In order to fix this you need to create an alias for each real user that points to itself.

```

INSERT INTO virtual VALUES ('5','realuser1@domain1.com','realuser1@domain1.com')

```

Once you add these Postifx will match the specific alias and deliver to the user account. If a specific alias does not exist it will fall back to the catchall alias. 

BTW, PostfixAdmin will handle all this for you and is a far better system. Unfortunately you'll have to completely change the db and a few other things. 

kashani

----------

## Cyberwizzard

I finally managed to implement the changes you proposed in the database queries (we already have a GUI so only the DB had to be adapted) and it worked  :Smile:  After some tweaking with indexes it ran only twice as slow as before ( << 100ms) so thats acceptable  :Smile: 

Thanks!

----------

## kashani

Remember to add into your interface a routine to automatically add an alias that points to itself when creating a real mailbox. PostfixAdmin allows you to turn that feature on or off. I generally turn it on and it leave it on so I never have to worry when a domain adds a catchall.

kashani

----------

## Cyberwizzard

Thats done automatically: the data is stored in a tree-like structure and managed the same way. The results presented to courier-authlib and postfix are a aggregation over multiple tables. Right now creating a new mailbox under a domain will automatically show up in the alias listing as well (mysql views ftw)   :Smile: 

----------

## ocbMaurice

Sorry to bring such an old thread to the surface, but this solved my "catch-all" problems.

I also have one mysql table with the real virtual users and one with the aliases.

The catchall defined in the alias table always took precedence over the actual user table.

Therefore all mails were going to the catch-all and none to the the actual user mailbox.

I prefer to keep my tables as clean as possible. Therefore I used another approach.

The Guide uses the fallowing Query to lookup the aliases:

```

# mysql-aliases.cf

user = mailsql

password = Passw0rd

dbname = mailsql

table = alias

select_field = destination

where_field = alias

hosts = unix:/var/run/mysqld/mysqld.sock
```

I changed that one to:

```

# mysql-aliases.cf

user = mailsql

password = Passw0rd

dbname = mailsql

table = alias

query = SELECT destination FROM t_mail_virtual_alias WHERE email='%s' UNION SELECT email FROM t_mail_boxes WHERE email='%s'

hosts = unix:/var/run/mysqld/mysqld.sock
```

So far this seems to do the job (I verified that the query is sent correctly).

Hope this is usefull for some other people. I'm not a mysql guru, so I don't know how much performance this needs.

Maurice

p.s. Gentoo Forum rocks, couldn't find a hint via google, only people with the same problem  :Wink: 

----------

