> I was trying to find a SQL example how to alias email that is addressed 

> to everyone at a secondary domain to my primary domain.  I mean:
> 
> I have many virtual users set up for example.com, I added a new domain, 
> example-2.com and I want any email going to example-2.com to be delivered to 
> example.com accounts.  I don't want to use a catchall like 
> @example-2.com because I want to reject invalid addresses.
> 
> There 
> are some other virtual domains on the server so the original query still
> needs to work the same.  The virtual_alias_maps table is two 
> columns: addr, dest and of course there may be some addr/dest entries 
> for example.com which should work transparently for example-2.com also
> 
> 
> I
> saw some posts saying it should be easy and there are examples, but I 
> can't find the examples.  I tried changing my virtual_alias_maps query 
> to something like this:
> 
> query=(select dest from aliases where addr = '%s') union (select 
> '%u...@example.com' from dual where '%d' = 'example-2.com') 
> limit 1
> 
> but it acts the same a having a catchall.
> 
> So I tried this
> 
> query=select dest from aliases where (addr = '%s' or ('%d' = 
> 'example-2.com' and addr = '%u...@example.com'))
> 
> 
> but it only works if there is an alias entry for the address in the table for 
> the address under example.com
> 
> Maybe the second one is OK but also requires to change my 
> virtual_mailbox_maps 
> to account for the secondary domain?  Like this?
> 
> query=select mail from users where (addr = '%s' or ('%d' = 
> 'example-2.com' and addr = '%u...@example.com'))
> 
> Even
> if that works (it does not - the lookups of valid/invalid recipients 
> works better (correctly) but the original address doesn't get rewritten 
> so virtual delivery ultimately fails), is there a way to do it ONLY in 
> the alias map?

I'm not sure if I'm on the right track, but the only thing I can think of is I 
have to check in the query if there is an alias and if not, if there is a 
regular account on the primary domain before I decide to re-write it. 
Pseudocode:


IF %d = example-2.com, THEN
    // first check if alias exists, if so, return it
    IF EXISTS IN alias map, THEN SELECT %u...@example.com FROM alias map
    ELSE
        // (account query but using primary domain; returning addr instead of 
mail)
        SELECT addr FROM accounts WHERE addr = %u...@example.com
    ENDIF
ELSE
    // (normal query)
    SELECT dest FROM aliases WHERE addr = %s
ENDIF 


So I came up with this:

query = select if ('%d' = 'example-2.com', IFNULL((select dest from aliases 
where addr = '%u...@example.com'), (select addr from users where addr = 
'%u...@example.com')), (select dest from aliases where addr = '%s'))

This seems to work in all cases except when I send to 
"postmas...@example-2.com" because I have an alias with just the local part:

postmaster   r...@example.com

This causes a bounce instead of reject. Do I have to add a clause for this to 
my query? I start to feel like I'm doing things Postfix should be doing. There 
must be a more simple way to do this?

Reply via email to