3

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

4

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.
  • I think it does the same number of queries than mine, since it's more readable I'll accept your solution – Lluís Oct 20 '14 at 8:26
  • In addition with this solution generic alias still work (@domain --> mail@domain) – Lluís Oct 20 '14 at 9:21
2

Finally this is what I configured

in /etc/postfix/mysql-virtual-aliases.cf:

hosts = localhost
user = postfix
password = mypass
dbname = postfix

query = SELECT goto FROM alias WHERE address = '%s'

in /etc/postfix/mysql-virtual-aliases-postmaster.cf:

hosts = localhost
user = postfix
password = mypass
dbname = postfix

query = SELECT 'me@mydomain' AS goto FROM domain WHERE
        domain='%d' AND active = '1' AND
        '%u' REGEXP '^(MAILER-DAEMON|postmaster|abuse|webmaster)$' LIMIT 1

and in main.cf:

virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-aliases.cf,
                     mysql:/etc/postfix/mysql-virtual-aliases-postmaster.cf

Maybe someone knows an easier/efficent way?

  • What's the purpose of alias and domain in your query above? It would be useful if you post how your virtual_mailbox_domains table looks like... – masegaloeh Oct 18 '14 at 14:27
  • If my assumption (about table structure) was right, maybe this query will works select 'me@example.com' from domain where domain='%d' AND '%u' IN ('MAILER-DAEMON','postmaster','abuse','webmaster'); – masegaloeh Oct 18 '14 at 14:54
  • I updated my question with alias table. – Lluís Oct 19 '14 at 20:12
  • 1
    @masegaloeh the query should return real goto value for other addresses – Lluís Oct 19 '14 at 20:14
  • I've fixed SQL query, now it's working, but maybe there's a better way to do this, this long query will be executed for every mail... – Lluís Oct 20 '14 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.