Retaining recipient address when forwarding using postfix

emailmail-forwardingpostfix

I'm setting up postfix to forward e-mails sent to a domain to outside e-mail addresses. i.e. when I get an e-mail at philip@contoso.com it gets sent automatically to philip@example.com. That's working except that when I receive the e-mail it shows the recipient at philip@example.com instead of philip@contoso.com.

I know it's possible to do this because when I had e-mails forwarded using some web panel on a cheapo hosting company, it worked great. So how do I get postfix to retain the recipient address even though it's being forwarded to another address?

My config:

alias_database = hash:/etc/aliases
alias_maps = hash:/etc/aliases
append_dot_mydomain = no
biff = no
config_directory = /etc/postfix
home_mailbox = Maildir/
inet_interfaces = all
inet_protocols = all
mailbox_command =
mailbox_size_limit = 0
mydestination = mydomain.com, localhost.localdomain, localhost
myhostname = mx1.mydomain.com
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
myorigin = /etc/mailname
readme_directory = no
recipient_canonical_maps = hash:/etc/postfix/recipient_canonical
recipient_delimiter = +
relayhost = [smtp.mandrillapp.com]
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache
smtp_use_tls = yes
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
smtpd_tls_cert_file = /etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file = /etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtpd_use_tls = yes

To be clear, when I receive the e-mail at the forwarded address, I still want it to show the To: address as having the original recipient (@mydomain.com).

Best Answer

Looks like you use this parameter

recipient_canonical_maps = hash:/etc/postfix/recipient_canonical

to provide mail forwarding. By default Canonical address mapping will rewrite both header and envelope address. So, disable header rewriting, you have two options here

  1. Configure recipient_canonical_classes so recipient_canonical_maps affects envelope address only.

    recipient_canonical_classes = envelope_recipient
    
  2. Use virtual_alias_maps that designed for envelope address mapping without touching header.

The virtual_alias_maps option is preferred in this case as you can use it to perform one-to-many mapping. Parameter recipient_canonical_maps only support one-to-one mapping.