Using a relay host with Sendmail
This article explains how to configure a relay host with Sendmail. Like everything else involving sendmail, it isn't as simple as it should be.
Configuring sendmail to use a relay host for all outgoing mail
The change involves two steps:
- You need to add a line to /usr/src/etc/sendmail/freebsd.mc
- Then you need to compile a new sendmail.cf and overwrite the existing one.
Make a new .mc file
First make a copy of the default freebsd.mc file by running the following commands:
$ sudo cp /usr/src/etc/sendmail/freebsd.mc /usr/src/etc/sendmail/freebsd-smarthost.mc
'Note: If you already have a non-standard .mc file that your current sendmail.cf file is built from, you need to add the new line to that .mc file and not a copy of the default .mc file, or you will loose your other changes!
It doesn't matter what you call the new .mc file. Open it up in an editor and add the following lines in the bottom (replacing doobie.tyknet.cn.dom. with your own relay host, of course):
There is a few things worth noting about the way sendmail handles the name of the relay/smart host.
First of all, if you dont fully qualify the hostname by adding the last dot after the hostname, sendmail will loop through all your search domains before trying to resolve the fully qualified name. This means a lot of unneeded DNS queries.
Secondly, if you don't put the hostname in the square brackets, sendmail will first try to find an MX record for the hostname, before falling back to the A record. This, combined with the search domains thing mentioned above, has for some reason led to my sendmail reporting dns lookup failures when trying to resolve the hostname of the relay server, presumably because it tries to resolve a bunch of things that doesn't resolve:
Jul 7 13:37:19 ildipiben sm-mta: n67BbJvB027184: to=<email@example.com>, ctladdr=<firstname.lastname@example.org> (1001/1001), delay=00:00:00, xdelay=00:00:00, mailer=relay, pri=30390, relay=doobie.tyknet.cn.dom., dsn=4.0.0, stat=Deferred: Name server: doobie.tyknet.cn.dom.: host name lookup failure
Anyway, after adding the SMART_HOST line above to the .mc file, you need to compile the new sendmail.cf file:
$ pwd /usr/src/etc/sendmail $ sudo make freebsd-smarthost.cf rm -f freebsd-smarthost.cf m4 -D_CF_DIR_=/usr/src/etc/sendmail/../../contrib/sendmail/cf/ /usr/src/etc/sendmail/../../contrib/sendmail/cf/m4/cf.m4 /usr/src/etc/sendmail/freebsd-smarthost.mc > freebsd-smarthost.cf chmod 444 freebsd-smarthost.cf
The new config file is called freebsd-smarthost.cf and is either in /usr/obj/usr/src/etc/sendmail/freebsd-smarthost.cf or in /usr/src/etc/sendmail/freebsd-smarthost.cf (the current directory). Now you just need to copy it to /etc/mail/sendmail.cf overwriting the one currently being used by sendmail, and restart sendmail for the changes to take effect. Remember to backup the current sendmail.cf before overwriting it. Then try sending a mail, and check the maillog to see if it used the correct relay server:
$ sudo cp /etc/mail/sendmail.cf /etc/mail/sendmail.cf.orig $ sudo cp /usr/obj/usr/src/etc/sendmail/freebsd-smarthost.cf /etc/mail/sendmail.cf $ sudo /etc/rc.d/sendmail restart Stopping sendmail_submit. $ echo "testing sendmail smart host" | mail -s "testing sendmail smart relay stuff" email@example.com
I checked /var/log/maillog with tail and found the following line amongst the rest of the normal logging, which tells me that it is now using the relay I specified, and also that it managed to speak TLS with the Postfix relay server:
Jul 7 18:38:45 tempwall sm-mta: STARTTLS=client, relay=doobie.tyknet.cn.dom., version=TLSv1/SSLv3, verify=FAIL, cipher=DHE-RSA-AES256-SHA, bits=256/256
--Tykling 16:52, 7 July 2009 (UTC)