Configure Postfix to use external MX servers for delivery of local mail if user is unknown

I have a following setup:

  • linux box with postfix configured to be responsible for example.com domain
  • domain's MX servers are configured so that mail sent to example.com is sent to google mail servers
  • several user accounts on linux machine exist (same machine also hosts example.com site)

When someone from the outside attempts to send mail to address ending with @example.com, it gets routed to google mail (and there handled appropriately).

When linux machine tries to send mail to outside world, mail is delivered correctly, as reverse dns and spf records are configured correctly, so linux machine is valid mail sender for example.com domain (along with google mail servers).

However, here's the problem. When php application (hosted at linux box) tries to send mail to [email protected] (and someuser doesn't exist on linux box), it fails, since it doesn't even consult google mail servers, but postfix smtp locally concludes that "someuser" is unknown.

So, the question is: how do I tell postfix to relay mails sent to @example.com domain to google mail servers (so, to servers specified in MX records), IF and only if a mailbox is not found locally.


Solution 1:

Do you need the postfix server to be responsible for example.com? (Do you ever want mail to be delivered there instead of to google?) If you always want mail for local example users to go to their google, remove $mydomain from the mydestination parameter in main.cf. This will not alter where the mails appear to be coming from for outside users. (That is controlled by "myorigin")

Since postfix either thinks it is responsible for the domain or not, there is no easy way to split the domain up so some mail gets delivered locally and some gets delivered to google. You can do some tricks by putting in forwarding rules or with transport mapping, but then you'd need to specify google or local for each user. I kind of doubt you really want to do that.

Solution 2:

Did you set "relayhost" in main.cf ?

Here is part of main.cf

myhostname = example.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = localhost
relayhost = googlemail.host

Mails send to [email protected] will be relayed to googlemail.host

In /etc/aliases you can provide an alias for the root user for local delivery, like

root: [email protected]