This post aims to cover two sets of questions that frequently appear on Serverfault:
“I have the email of my organization hosted at Google and the web server at a hosting provider. When the web server sends email (when a form is completed for example), email is received by everyone except when the recipient is in our domain. Then sendmail tries to deliver locally and not over at Google”. Or, “certain recipients, including Google, reject email from the web server (or servers withing our LAN) as spam”.
There are answers at Serverfault recommending the use of ssmtp in order to forward all sending email via Google, but this requires SMTP authentication and a password saved in a file.
For the purposes of this post the domain example.com will be used.
Configure SPF for example.com
SPF is framework that allows the domain name owners notify the world who they believe the appropriate servers sending mail on behalf of their domain are. Google support pages note that the SPF record should at least be in the form of v=spf1 include:_spf.google.com ~all. However, it is also needed that server.example.org be able to send email on behalf of example.com. So the appropriate record becomes:
v=spf1 a:server.example.org include:_spf.google.com ~all
Note: example.org is not the same domain as example.com
Configure sendmail for server.example.org
example.com is included in /etc/mail/local-host-names, which means that server.example.org treats this a local domain and will try to deliver locally, instead of Google. The following additions to the sendmail configuration file (sendmail.mc) take care of this:
LOCAL_CONFIG Kbestmx bestmx -T.TMP LOCAL_RULE_0 R $* < @ example.com. > $* $#esmtp $@ [$(bestmx example.com. $)] $: $1 < @ example.com. > $2
The line is broken in two for readability. As always remember that the LHS and the RHS of the rule are separated with tabs and not spaces. So do not copy-paste. Build and install sendmail.cf, restart sendmail and check.