nhebb
By:
nhebb

How to correctly configure Postfix to route emails to aliases?

August 26, 2014 7.1k views

For the most part, I have successfully configured Postfix to send emails. Emails sent to outside domains, e.g. example@gmail.com, are delivered fine. The problem I'm having is when I send emails from my website's contact form, via the PHP mail() function, to my domain's own email addresses.

Here are the problems I'm experiencing:

  • Emails sent to my user name, e.g. myname@mydomain.com go to /var/mail/myname but are not sent to the Google Apps account that my MX records point to. They also have the return path header overwritten to be www-data@mydomain.com.
  • Emails sent to aliases, e.g. support@mydomain.com, are sent to /var/mail/myname with a message: Action: failed, Status: 5.1.1, Diagnostic-Code: X-Postfix; unknown user: "support". The error message shows that the return path header has been overwritten, but in this case to
  • All the emails have the return path header information overwritten. In the php mail() function, the return path is set to support@mydomain.com but is changed to myname@mydomain.com in the emails that result in an error and www-data@mydomain.com in the emails that get successfully delivered.

Configuration Notes

/etc/postfix/main.cf:

  • myorigin = mydomain.com
  • mydestination = mydomain.com, localhost.mydomain.com, , localhost

/etc/aliases:

/etc/postfix/virtual

I ran postmap on /etc/postfix/virtual and /etc/init.d/postfix reload before attempting the send the emails.

1 comment
  • It's too late to edit my original post, but I also wanted to add that I included a wildcard @mydomain.com in the /etc/postfix/virtual.

    Plus, I tried changing the /etc/postfix/main.cf file:
    alias_maps = hash:/etc/aliases

    To:
    virtualaliasmaps = hash:/etc/postfix/virtual

1 Answer

You mentioned that your contact form sends emails to your local user as defined in your postfix/virtual file but you also mention that you want them forwarded to Gmail which is what your MX records are set to point to.

If you have set your MX records to Gmail I'm assuming that you want Gmail to handle your inbox so why did you define local mailboxes as well?

Outside of that though, the reason that they are being delivered to the local in-box is because if your sending through postfix it will check its internal routes before it looks up the MX records and so it will successfully deliver that email locally.

Have another answer? Share your knowledge.