postfix 2.9.6.1 forward all mail to an external mail address

postfix-mta, ubuntu

Solution

Finally found the answer.

Listing here for posterity.

Add the folowing line to the end of your main.cf file

vi /etc/postfix/main.cf 

virtual_maps = hash:/etc/postfix/virtual, regexp:/etc/postfix/virtual-regexp

Create a file which lists the mail address you want all mails to be sent to

vi /etc/postfix/virtual-regexp

Add the following:

/.+@.+/ forwardingmailaddress@gmail.com

add it to postmap

postmap /etc/postfix/virtual-regexp

you will need to add a virtual file.

touch /etc/postfix/virtual

now add it to postmap

postmap /etc/postfix/virtual

Problem

I am trying to tell postfix that all mails to any address should be forwarded to an external email address. My main.cf includes the entry for ``` smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu) biff = no # appending .domain is the MUA's job. append_dot_mydomain = no # Uncomment the next line to generate "delayed mail" warnings #delay_warning_time = 4h readme_directory = no # TLS parameters smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key smtpd_use_tls=yes smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache # See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for # information on enabling SSL in the smtp client. myhostname = xshaunm-Q1532N alias_maps = hash:/etc/aliases alias_database = hash:/etc/aliases mydestination = xshaunm-Q1532N, localhost.localdomain, , localhost relayhost = mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 mailbox_size_limit = 0 recipient_delimiter = + inet_interfaces = all virtual_alias_domains = xshaunm-Q1532N.pvt.lan virtual_alias_maps = hash:/etc/postfix/virtual ``` My /etc/postfix/virtual looks as follows: ``` (.*) testaddress@gmail.com ``` I then run the following command postmap /etc/postfix/virtual restart postfix /etc/init.d/postfix restart Now if I run the command ``` echo test | sendmail test@mydomain.com ``` it should deliver to testaddress@gmail.com, but for some reason it delivers to test@mydomain.com, which is incorrect If i list the exact address in the /etc/postfix/virtual as follows then it works, but there are hundreds of mail addresses it needs to catch and forward, so a regular expression would be better: ``` test@mydomain.com testaddress@gmail.com ```

Original source