Adding Global Alias in virtual_alias_maps for Local Domains Only

debianpostfix

I want to create a global alias for all my domains to catch all mail to addresses like postmaster@

I've added theses lines to aliases.pcre:

/(MAILER-DAEMON|postmaster|abuse|webmaster)@(.*)$/  me@mydomain

and in main.cf

virtual_alias_maps = pcre:/etc/postfix/aliases.pcre

but it catches mails to external domains too, i.e. redirecting to me mails sent to postmaster@external.com

Can I redirect all mail sent to theses addresses but only if domain is in virtual_mailbox_domains? (without writing the domain names in aliases.pcre

* update *

users, domains, alias.. are stored in a database, using postfixadmin to manage it.

alias tables looks like this:

mysql> describe alias;
+----------+--------------+------+-----+---------------------+-------+
| Field    | Type         | Null | Key | Default             | Extra |
+----------+--------------+------+-----+---------------------+-------+
| address  | varchar(255) | NO   | PRI | NULL                |       |
| goto     | text         | NO   |     | NULL                |       |
| domain   | varchar(255) | NO   | MUL | NULL                |       |
| created  | datetime     | NO   |     | 0000-00-00 00:00:00 |       |
| modified | datetime     | NO   |     | 0000-00-00 00:00:00 |       |
| active   | tinyint(1)   | NO   |     | 1                   |       |
+----------+--------------+------+-----+---------------------+-------+

ideally it should respect alias from database if it's set, and only redirect mail for addresses without alias

Best Answer

As alternative, instead long query as proposed above you can split to two sql maps

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-aliases.cf, mysql:/etc/postfix/my-custom-query.cf

Both files has same parameter except the query. For /etc/postfix/mysql-virtual-aliases.cf, you can use default query from postfixadmin

SELECT goto FROM alias WHERE address = '%s'

And for my-custom-query.cf you have query

select 'me@example.com' AS goto from domain where domain='%d' AND '%u' REGEXP '^(MAILER-DAEMON|postmaster|abuse|webmaster)$' LIMIT 1;

How it works

If your first maps mysql-virtual-aliases.cf returns null, then postfix will attempt to query to second maps my-custom-query.cf.

Pros:

  • If your email address match query in mysql-virtual-aliases.cf, then you just run one simple query.

Cons:

  • If your email address doesn't match query in mysql-virtual-aliases.cf, you must run additional second query.