How to force mail server to send emails outside of local host
After many years in this business, or I should say profession, experience gives you the ability to understand how things really work and come to conclusions that you would have never thought because it just makes no sense. These conclusions become theories until you are able to prove them.
It started when 8 years ago I was hosting all our projects on a shared server and I moved our business website and emails to a separate VPS. Everything was working smoothly until one day, working on a contact form for a client we realized that something was oddly weird. While testing the form everybody was able to receive a confirmation email, gmail, hotmail, yahoo, our clients but us. Only when using our email address @ourdomain.com the form wouldn't work.
We simply ignored the problem since everything was working for our client and I thought it must be a SPAM filter on our server. A few months later I understood what was going on. Remember that I used to have our website hosted on the same shared hosting? Well, it turns out that I never removed it from the shared hosting. The NS records were pointing to the new server, but the old server still had a DNS zone for the website even if it was not active. When I went into the webmail of the old website and look at the inbox folder, I found the answer to the mystery, all the tests emails from the form were there. This explained to me how servers handle things internally when DNS zones are managed locally and it all made sense.
Later I faced a new problem, as I got more experienced and we had more websites to handle, the answer to a better service was getting dedicated servers, and from bad experiences, we also learned to separate websites from emails by having them on separate servers, just in case one goes down the other one will continue to work.
For quite a long time we have used third-party companies as outgoing servers when sending emails from our web projects, simply to ensure that they would not go into spam and to never get our IP's blacklisted (for more information look for transactional email providers like Mailchimp's Mandril or Godaddy's Madmimi). But one day we needed to host web service developed by another company that was sending email confirmations with the old-fashioned mail() function from php. What happened next was simply a deja vua and the voice of experienced kicked in.
Everybody could receive the emails except the client.... I knew exactly what the problem was but this time I had no idea how to fix it. The website was hosted on one server, the emails were on another server and I knew that the web server was trying to deliver the emails locally.So the first question that I asked google was:
Why WHM delivers emails locally?
WHM is the web host manager by CPanel, and that is how the research began, I didn't get the answers that I was looking for, I tried changing the local DNS zones and point the MX records to the new server, but it didn't work, I was not getting the answer that I was looking for until I made the following question:
How to disable local mail delivery?
That's when I found the answer:
When creating new accounts in WHM at the bottom you will find Mail Routing Settings, I always ignored this setting, I always went for the default setting "Automatically Detect Configuration (recommended) " and I always thought that this was to generate the local DNS records, but this was really the answer to the mystery. By choosing "Local Mail Exchanger" the emails are always delivered locally, but by choosing "Remote Mail Exchanger" emails are always send out the real mail server.
Now the new problem was when accounts were already created, so the following question found the answer:
How to change Mail Routing Settings of existing accounts (WHM)?
It turns out that it was always in front of me, at the bottom of the DNS Zone page ( Main >> DNS Functions >> Edit DNS Zone.) We find again the Email Routing Settings where we can choose "Remote Mail Exchanger" and solve the problem once and for all.