The Postfix mail transfer agent (MTA) provides optional lookup tables that alias specific mail addresses or domains to other local or remote addresses. In the main.cf configuration file of Postfix you can define a lookup table (a mapping file) by using the virtual_alias_maps keyword:
1 | virtual_alias_maps = hash:/etc/postfix/virtual |
In the assigned mapping file (e.g. virtual) there is now space to set up the forwarding of a mail address (pattern) to another mail address (result):
1 2 | info@example.com webmaster@example.com mail@example.com webmaster@example.com |
To enable this configuration you have to execute postmap virtual and postfix reload. As you can see here, every time the mapping has changed, somebody must create the hash table and reload the entire MTA using these commands. To avoid this, Postfix supports MySQL mapping. Here the forwarding configuration is not coming from a mapping file anymore but from a MySQL table. Then you’ll be able to dynamically add, edit or remove forwardings without calling the commands above. In Debian / Ubuntu the postfix-mysql package is required. In the main.cf configuration file the definition of the lookup table (mapping file) changes as follows:
1 | virtual_alias_maps = mysql:/etc/postfix/mysql-virtual-alias-maps.cf |
In the assigned mapping file (e.g. mysql-virtual-alias-maps.cf) there is now space to set up the bridge for connecting Postfix and MySQL (using the well-known parameters plus a query):
1 2 3 4 5 | user = myuser password = mypassword hosts = 127.0.0.1 dbname = mydb query = SELECT result FROM forwardings WHERE pattern = '%s' |
Last but not least just create the forwardings table in your MySQL database. Adding rows to that table is like adding forwardings in the mapping file above. And again, no reload etc. is required because Postfix queries the forwardings on demand.
1 2 3 4 5 6 7 | CREATE TABLE forwardings ( pattern VARCHAR(255), result VARCHAR(255) ); INSERT INTO forwardings VALUES ('info@example.com', 'webmaster@example.com'); INSERT INTO forwardings VALUES ('mail@example.com', 'webmaster@example.com'); |








