postfix_virtual_mail_addresses_with_regular_expressions

Postfix: virtual mail addresses with regular expressions

in the early days if you needed to have lots of email adresses all forwarded to the same target address and you did not want to bother to create each and every address specifically in your virtual_address_map you would simply create a catch-all address and you where good to go. however, with all the spaming happening today, we need another solution.

let's say you have a ticketing system that creates a “reply-to” address with the case-id in the address itself to track the case even if the subject changes or any other reference is lost. these dynamically assigned addresses all start with “suport-ticket” and end on “@mydomain.com”. now you could create a mysql table and use the mysql postmap connector to dynamically “create” these addresses. another opption is to create a single entry with a regular expression, allowing all support-ticket-<ticket-id>@mydomain.com mails to be forwarded to a certain email address.

this can be done with the regexp table of postfix. in your main.cf look for the virtual_alias_maps line and add a new regexp map to the list (comma separated): virtual_alias_maps = <whatever you had here before>, regexp:/etc/postfix/support-ticket.map

now create a new file in /etc/postfix/support-ticket.map with the following contents:

/^support-ticket-.*@mydomain.com$/ tickets@mydomain.com

to test if your regex works as you expected, you can now use the postmap command with the -q (query) option:

postmap -q support-ticket-bla@mydomain.com regexp:/etc/postfix/support-ticket.map

should now return your tickets@mydomain.com address

if that is so, run postmap to update the database and reload postfix

postmap /etc/postfix/support-ticket.map
postfix reload 
  • postfix_virtual_mail_addresses_with_regular_expressions.txt
  • Last modified: 20.01.2019 11:34
  • by Pascal Suter