问题
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
回答1:
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
回答2:
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:
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 foo@example.com" | sendmail foo@example.com
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
来源:https://stackoverflow.com/questions/18270439/postfix-2-9-6-1-forward-all-mail-to-an-external-mail-address