TIL Default Sendmail hostname gotcha

This is a gotcha if you are using anything that hooks up the server’s mail command like mail() function in PHP.

Let’s say your default mail provider for example.com is hosted on a server different than your main application at example.com. Sendmail will use the first hostname in the /etc/hosts file as the default hostname. So if you set the server hostname to example.com and try to send email from the application it will not be delivered properly and will result in a lot of dead.letter(s).

A quick solution to this is to change the line example.com localhost in /etc/hosts to www.example.com localhost and reboot the server. If you take this route make sure you also remove the example.com from /etc/mail/local-host-names.