postfix 2.9.6.1 forward all mail to an external mail address

rezizter picture rezizter · Aug 16, 2013 · Viewed 9.7k times · Source

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:

(.*) [email protected]

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 [email protected]

it should deliver to [email protected], but for some reason it delivers to [email protected], 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:

[email protected] [email protected]

Answer

richb-hanover picture richb-hanover · May 1, 2014

I had similar trouble forwarding to my Gmail account. I had one host running postfix, forwarding multiple virtual domains. Even though I was pretty sure that I had set the config files (/etc/postfix/virtual, /etc/postifx/main.cf) properly, the messages would sometimes arrive, but sometimes I'd get bounce messages.

To get it working, I also had to:

  1. Send test messages from some other account. Gmail appears to discard messages that come to it having been sent from the same mailbox/address because it thinks there's a mail loop. Send your test message from some other account, or even use the following from the host where you're running postfix.

    echo "test to [email protected]" | sendmail [email protected]
    
  2. Add a SPF record for the host that's forwarding the mail (running postfix). A SPF record indicates to other computers that the domain owner says that the relaying host is allowed to send mail for that domain. The basics of SPF are at OpenSPF: http://www.openspf.org/Introduction. Google has a good writeup at https://support.google.com/a/answer/33786  

    TL;DR Create a TXT record containing this text: v=spf1 mx include:_spf.google.com ~all